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 ...
随机推荐
- SQLSERVER 创建ODBC 报错的解决办法 SQLState:'01000'的解决方案
错误详情如下: SQLState:'01000' SQL Server 错误:14 [Microsoft][ODBC SQL Server Driver][DBNETLIB] ConnectionOp ...
- NopCommerce 3.80框架研究(三)替换tinymce 为KindEditor
NopCommerce 自带的编辑器tinymce 功能不是很全.所以尝试将其替换为功能更强大的 KindEditor 并替实现文件上传和在线浏览功能 首先下载 并解压到如下位置 请注意这里是部署在N ...
- HDU3973 线段树 + 字符哈希
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3973 , 线段树 + 字符哈希,好题. 又学了一种新的哈希方法,hhhh~ 解法: 想法是用P进制的数 ...
- Head First HTML与CSS阅读笔记(二)
上一篇Head First HTML与CSS阅读笔记(一)中总结了<Head First HTML与CSS>前9章的知识点,本篇则会将剩下的10~15章内容进行总结,具体如下所示. div ...
- JS中的toString()和valueOf()方法
1.toString()方法:主要用于Array.Boolean.Date.Error.Function.Number等对象转化为字符串形式.日期类的toString()方法返回一个可读的日期和字符串 ...
- vuejs里面v-if,v-show和v-for
<div id='root'> <div v-if='show'>helle world</div> <button @click='handleClick' ...
- 【转载】WEB架构师成长之路
本人也是coding很多年,虽然很失败,但也总算有点失败的心得,不过我在中国,大多数程序员都是像我一样,在一直走着弯路,如果想成为一个架构师,就必须走正确的路,否则离目标越来越远,正在辛苦工作的程序员 ...
- Java中ArrayList的对象引用问题
前言事件起因是由于同事使用ArrayList的带参构造方法进行ArrayList对象复制,修改新的ArrayList对象中的元素(对象)的成员变量时也会修改原ArrayList中的元素(对象)的成员变 ...
- AngularJs学习笔记-表单处理
表单处理 (1)Angular表单API 1.模板式表单,需引入FormsModule 2.响应式表单,需引入ReactiveFormsModule (2)模板式表单 在Angular中使用for ...
- AngularJS最佳实践
1.依赖注入不要用推断式 2.双向绑定的变量设置成$scope下的一个对象的属性 3.多个控制器之间的通信尽量使用service实现,不要使用全局变量或者$rootScope 4.尽量不在控制器中操作 ...