poj1111(单身快乐)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 9183 | Accepted: 5426 |
Description
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 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
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(单身快乐)的更多相关文章
- 6、HTML5表单提交和PHP环境搭建
---恢复内容开始--- 1.块元素 块元素在显示的时候,通常会以新行开始 如:<h1> <p> <ul> <!-- 块—>注释 <p>he ...
- 5、HTML5列表、块和布局
1.块元素 块元素在显示的时候,通常会以新行开始 如:<h1> <p> <ul> <!-- 块—>注释 <p>hello</p> ...
- 江西理工大学南昌校区排名赛 E: 单身狗的种树游戏
题目描述 萌樱花是一只单身狗. 萌樱花今天决定种树,于是他来到了自己家的后院. 萌樱花的后院有n个树坑,所有树坑排列在一行上,每一个树坑都可以种一棵树,相邻树坑间的距离为1,现在所有的树坑都是空着的. ...
- 背后的故事之 - 快乐的Lambda表达式(一)
快乐的Lambda表达式(二) 自从Lambda随.NET Framework3.5出现在.NET开发者眼前以来,它已经给我们带来了太多的欣喜.它优雅,对开发者更友好,能提高开发效率,天啊!它还有可能 ...
- Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录
一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...
- 背后的故事之 - 快乐的Lambda表达式(二)
快乐的Lambda表达式 上一篇 背后的故事之 - 快乐的Lambda表达式(一)我们由浅入深的分析了一下Lambda表达式.知道了它和委托以及普通方法的区别,并且通过测试对比他们之间的性能,然后我们 ...
- C#-正则,常用几种数据解析-端午快乐
在等待几个小时就是端午节了,这里预祝各位节日快乐. 这里分享的是几个在C#中常用的正则解析数据写法,其实就是Regex类,至于正则的匹配格式,请仔细阅读正则的api文档,此处不具体说明,谢谢. 开始吧 ...
- [LeetCode] Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- WPF 画心2.0版之元旦快乐
2017年元旦已经到了,想做一个祝福语的窗口,就把上一篇画心的程序改了改,变成了如下界面. 说下改动的地方,首先窗口没有标题栏了. MainWindow.xaml AllowsTransparency ...
随机推荐
- CheckPoint_vSEC_Cluster_R77.30
CheckPoint_vSEC_Cluster_R77.30 平台: arm 类型: ARM 模板 软件包: Check Point vSEC Gateway R77.30-041.161 Anti- ...
- python 函数学习之sys.argv[1]
一.sys 模块 sys是Python的一个「标准库」,也就是官方出的「模块」,是「System」的简写,封装了一些系统的信息和接口. 官方的文档参考:https://docs.python.org/ ...
- 洛谷 P1077 摆花
题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能超过ai盆,摆花时 ...
- linux 命令——18 locate (转)
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...
- Select与SelectMany
SelectMany在MSDN中的解释:将序列的每个元素投影到 IEnumerable(T) 并将结果序列合并为一个序列. 不用去用foreach进行两次遍历,就可以将子循环需要的元素过滤出来... ...
- hdu-1532 Drainage Ditches---最大流模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 给出有向图以及边的最大容量,求从1到n的最大流 思路: 传送门:最大流的增广路算法 ...
- python_21_copy
import copy person=['name',['saving',100]] #3种浅copy方式 p1=copy.copy(person) p2=person[:] p3=list(pers ...
- 【luogu P5022 旅行】 题解
题目连接:https://www.luogu.org/problemnew/show/P5022 \(NOIP2018 DAY2T1\) 考场上只写了60分,很容易想到当 m = n - 1 时的树的 ...
- Angular2的笔记
1.如果启动项目的时候出现下列黄色的警告说明电脑安装的全局cli和项目中使用的cli版本不一致,不过不影响使用,按它的提示执行 ng set --global warnings.versionMism ...
- webpack 4.x 解决 webpack-dev-server工具在webpack构建的项目中使用问题
webpack-dev-server工具能实现自动打包编译和热更新 首先将webpack-dev-server安装到项目中 npm install webpack-dev-server -D 这时在命 ...