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 ...
随机推荐
- IT技能等级
IT技能 低:会使用,会简单维修(操作) 中:能跟据实际业务需求扩展(技改.完善) 中高:找产品,能组合,能设计创造(出方案) 高:能规划(规划)
- linux性能测试脚本
http://linux-bench.com/ What is Linux-Bench? Linux-Bench is a simple script that provides a basic le ...
- System Center Configuration Manager 2016 必要条件准备篇(Part1)
步骤4.创建系统管理容器 SCCM 2016 配置管理系列(Part 1- 4) 介绍AD01上配置了Active Directory域服务(ADDS),然后将Configuration Manag ...
- SQLServer从其他表获取的数据更新该表的一部分
在网上常见的是update a set username = username FROM b on a.userid=b.userid,该更新语句是对a表中所有行进行更新.如果只更新一部 ...
- LeetCode Merge Two Sorted Lists 归并排序
题意: 将两个有序的链表归并为一个有序的链表. 思路: 设合并后的链表为head,现每次要往head中加入一个元素,该元素要么属于L1,要么属于L2,可想而知,此元素只能是L1或者L2的首个元素, ...
- 利用photoshop制作gif图片
首先准备你需要的几张素材图片 1.将素材图片根据发生的顺序放置在不同的图层 2.打开窗口下的时间轴 选择帧动画 3.创建第一张帧动画 选项卡右边这个按钮,点击这个选择新建帧 第一张图片显示其他的隐藏 ...
- html body上有一条空白!!!
html body 上莫名其妙的就出现了一条空白,怎么搞都搞不定,弄了一下午...... 解决了!!! 格式问题/
- 【BZOJ2002】[HNOI2010] 弹飞绵羊(大力分块)
点此看题面 大致题意: 有\(n\)个弹力装置,当到达第\(i\)个装置时,会被弹到第\(i+k_i\)个装置,若不存在第\(i+k_i\)个装置,就会被弹飞.有两种操作,一种操作是将\(k_x\)改 ...
- 移动端调试利器-vConsole
现在移动端开发越来越火,随之而来的问题也越来越多,今天给大家介绍一款移动端调试神器,vconsole. 一.先引用文件,可以从https://www.bootcdn.cn/vConsole/下载,也可 ...
- vue 服务代理 调用第三方api
项目中前期需要调用第三方API来获取汇率.因为直接调用会有跨域的问题,所以使用来服务代理. 在config配置代理可以这样写: 而调用接口就可以这样写: 坑:配置完成后一直报500,开始怀疑人生.最后 ...