Image Perimeters
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9183   Accepted: 5426

Description

Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected for analysis by a mouse click on the object. The perimeter of the boundary of an object is one useful measure. Your task is to determine this perimeter for selected objects.

The digitized slides will be represented by a rectangular grid of
periods, '.', indicating empty space, and the capital letter 'X',
indicating part of an object. Simple examples are

XX   Grid 1       .XXX   Grid 2

XX .XXX

.XXX

...X

..X.

X...

An X in a grid square indicates that the entire grid square,
including its boundaries, lies in some object. The X in the center of
the grid below is adjacent to the X in any of the 8 positions around it.
The grid squares for any two adjacent X's overlap on an edge or corner,
so they are connected.

XXX

XXX Central X and adjacent X's

XXX

An object consists of the grid squares of all X's that can be linked
to one another through a sequence of adjacent X's. In Grid 1, the
whole grid is filled by one object. In Grid 2 there are two objects.
One object contains only the lower left grid square. The remaining X's
belong to the other object.

The technician will always click on an X, selecting the object
containing that X. The coordinates of the click are recorded. Rows and
columns are numbered starting from 1 in the upper left hand corner.
The technician could select the object in Grid 1 by clicking on row 2
and column 2. The larger object in Grid 2 could be selected by clicking
on row 2, column 3. The click could not be on row 4, column 3.



One useful statistic is the perimeter of the object. Assume each X
corresponds to a square one unit on each side. Hence the object in
Grid 1 has perimeter 8 (2 on each of four sides). The perimeter for the
larger object in Grid 2 is illustrated in the figure at the left. The
length is 18.

Objects will not contain any totally enclosed holes, so the leftmost
grid patterns shown below could NOT appear. The variations on the right
could appear:

Impossible   Possible 

XXXX         XXXX   XXXX   XXXX

X..X XXXX X... X...

XX.X XXXX XX.X XX.X

XXXX XXXX XXXX XX.X ..... ..... ..... .....

..X.. ..X.. ..X.. ..X..

.X.X. .XXX. .X... .....

..X.. ..X.. ..X.. ..X..

..... ..... ..... .....

Input

The input will contain one or more grids. Each grid is preceded by a line containing the number of rows and columns in the grid and the row and column of the mouse click. All numbers are in the range 1-20. The rows of the grid follow, starting on the next line, consisting of '.' and 'X' characters.

The end of the input is indicated by a line containing four zeros.
The numbers on any one line are separated by blanks. The grid rows
contain no blanks.

Output

For each grid in the input, the output contains a single line with the perimeter of the specified object.

Sample Input

2 2 2 2
XX
XX
6 4 2 3
.XXX
.XXX
.XXX
...X
..X.
X...
5 6 1 3
.XXXX.
X....X
..XX.X
.X...X
..XXX.
7 7 2 6
XXXXXXX
XX...XX
X..X..X
X..X...
X..X..X
X.....X
XXXXXXX
7 7 4 4
XXXXXXX
XX...XX
X..X..X
X..X...
X..X..X
X.....X
XXXXXXX
0 0 0 0

Sample Outpu8 18 40 48 8

题意:这道题可以看作一个感染的样例,给定一个矩阵,指定一个母体X的位置,处在这个X周围8个位置内的X都会被感染,被感染的X仍可以感染它周围8个位置,最后直到无法感染位置。要求的就是最后得到的被感染的图形周长。
分析图形可以知道,.相当于被感染X的隔离带,需要求的就是隔离带的长度,被感染的X周围有1个.,周长就会+1,最终的周长就是被感染的X周围.的和。

思路:将输入的矩阵周围用.包一圈,然后用DFS进行遍历,在遍历的同时统计被感染X周围的.数量,遍历结束得到周长

下面给出AC代码:

 #include<iostream>
#include<cstring>
using namespace std;
char M[][];
int dir[][] = { { , },{ , },{ ,- },{ -, },{ ,- },{ -,- },{ -, },{ , } };//→↑←↓↘↙↖↗
int DFS(int m, int n)
{
int sum = ;
if (M[m][n] == '' || M[m][n] == '.') return ;
M[m][n] = '';
for (int i = ;i < ;i++)
{
if (M[m + dir[i][]][n + dir[i][]] == '.')
sum++;
}
for (int i = ;i < ;i++)
{
sum = sum + DFS(m + dir[i][], n + dir[i][]);
}
return sum;
}
int main()
{
int l, w, x, y;
while (cin >> l >> w >> x >> y)
{
if (l == && w == )
return ;
memset(M, '.', sizeof(M));
for (int i = ;i <= l;i++)
for (int j = ;j <= w;j++)
cin >> M[i][j];
int sum;
sum = DFS(x, y);
cout << sum << endl;
}
return ;
}

这是我第一篇博客,为了庆祝我单身快乐,1111送给自己。

希望各位大大能够对我多指导指导,感谢~!感谢~~!感谢~~~!

poj1111(单身快乐)的更多相关文章

  1. 6、HTML5表单提交和PHP环境搭建

    ---恢复内容开始--- 1.块元素 块元素在显示的时候,通常会以新行开始 如:<h1> <p> <ul> <!-- 块—>注释 <p>he ...

  2. 5、HTML5列表、块和布局

    1.块元素 块元素在显示的时候,通常会以新行开始 如:<h1> <p> <ul> <!-- 块—>注释 <p>hello</p> ...

  3. 江西理工大学南昌校区排名赛 E: 单身狗的种树游戏

    题目描述 萌樱花是一只单身狗. 萌樱花今天决定种树,于是他来到了自己家的后院. 萌樱花的后院有n个树坑,所有树坑排列在一行上,每一个树坑都可以种一棵树,相邻树坑间的距离为1,现在所有的树坑都是空着的. ...

  4. 背后的故事之 - 快乐的Lambda表达式(一)

    快乐的Lambda表达式(二) 自从Lambda随.NET Framework3.5出现在.NET开发者眼前以来,它已经给我们带来了太多的欣喜.它优雅,对开发者更友好,能提高开发效率,天啊!它还有可能 ...

  5. Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录

    一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...

  6. 背后的故事之 - 快乐的Lambda表达式(二)

    快乐的Lambda表达式 上一篇 背后的故事之 - 快乐的Lambda表达式(一)我们由浅入深的分析了一下Lambda表达式.知道了它和委托以及普通方法的区别,并且通过测试对比他们之间的性能,然后我们 ...

  7. C#-正则,常用几种数据解析-端午快乐

    在等待几个小时就是端午节了,这里预祝各位节日快乐. 这里分享的是几个在C#中常用的正则解析数据写法,其实就是Regex类,至于正则的匹配格式,请仔细阅读正则的api文档,此处不具体说明,谢谢. 开始吧 ...

  8. [LeetCode] Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  9. WPF 画心2.0版之元旦快乐

    2017年元旦已经到了,想做一个祝福语的窗口,就把上一篇画心的程序改了改,变成了如下界面. 说下改动的地方,首先窗口没有标题栏了. MainWindow.xaml AllowsTransparency ...

随机推荐

  1. C#中split的方法汇总

    字符串的处理往往离不开split方法,下面介绍几种split的用法: 1.对单个字符进行分割(注意这里是字符,不是字符串,故只能用单引号‘’) string s=abcdeabcdeabcde; st ...

  2. pat甲级1139

    1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...

  3. linux 命令——52 ifconfig(转)

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  4. 同余问题(一)——扩展欧几里得exgcd

    前言 扩展欧几里得算法是一个很好的解决同余问题的算法,非常实用. 欧几里得算法 简介 欧几里得算法,又称辗转相除法. 主要用途 求最大公因数\(gcd\). 公式 \(gcd(a,b)=gcd(b,a ...

  5. 在TreeView控件节点中显示图片

    实现效果: 知识运用: TreeView控件中Nodes集合的Add方法 //创建节点并将节点放入集合中 public virtual TreeNode Add (string key,string ...

  6. Abode Dreamweaver cc 安装与激活

    原文链接Abode Dreamweaver CC是Adobe宣布放弃Creative Suite系列产品后推出的新版Creative Cloud产品,功能上修复了CS6中出现的选取代码不精准的问题,最 ...

  7. Drupal的入门学习

    1. 注意content中的区别 Article和Basic page的区别 a.输入字段不一样,Article内容多了两个字段:tag和图片. b.内容的默认设置不一样,Article默认允许评论, ...

  8. EL和JSTL表达式学习记录

    EL.JSTL表达式EL:1.EL使用目的:代替jsp页面中java脚本编写 2.EL作用:(1)从域中取出数据(最重要)(2)EL的内置对象(11个)(3)EL可以执行表达式运算 JSTL(JSP标 ...

  9. Java算法面试题:编写一个程序,将e:\neck目录下的所有.java文件复制到e:\jpg目录下,并将原来文件的扩展名从.java改为.jpg

    package com.swift; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; i ...

  10. .pyc是什么鬼

    .pyc是个什么鬼? 1. Python是一门解释型语言? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去,直到发现了*.pyc文件的存 ...