TOJ1334
1334: Oil Deposits
总提交: 555 测试通过:404
描述
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
输入
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
输出
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
样例输入
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
样例输出
0
1
2
2
题目来源
题解:
题目意思是判断油带的数量,相邻油田属于同一油带,相邻,是指两个小正方形区域上下、左右、左上右下或左下右上同为’@’。
简单深搜。
#include<iostream>
using namespace std;
int x,y,i,j,s;
char arr[101][101];
int g[8]={0,0,1,1,1,-1,-1,-1};
int q[8]={1,-1,0,-1,1,0,1,-1};
int dfs(int a,int b)
{
if(a<0||b<0||a>=x||b>=y)return 0;
if(arr[a][b]=='*')return 0;
arr[a][b]='*';
for(int k=0;k<8;k++)
{
dfs(a+g[k],b+q[k]);
}
}
int main()
{
while(cin>>x>>y)
{
if(x==0&&y==0)break;
s=0;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>arr[i][j];
}
}
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
if(arr[i][j]=='@')
{
s++;
dfs(i,j);
}
}
}
printf("%d\n",s);
}
}
TOJ1334的更多相关文章
随机推荐
- Mac上部署JDK/Ant/Jmeter/Jenkins
一.安装JDK 1. 下载JDK 2. 下完后直接双击安装,默认安装到/Library/Java/JavaVirtualMachine下 3. 验证是否安装成功 java -version 二.安装J ...
- Python Day04
一.迭代器与生成器: 迭代器(iterator): 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,迭代器的一大优点是不要求事 ...
- myeclipse编译问题
这个问题困扰了两天,在用springmvc编写Controller层的时候.我前台js跳转时@RequestMapping无法捕获到,但是之前的编写的都可以捕获正常执行.然后我更改js的跳转地址,发现 ...
- “如何稀释scroll事件”的思考(不小心写了个异步do...while)
看了下园友的一帖子:http://www.cnblogs.com/xzhang/p/4145697.html#commentform 本来以为是很简单的问题,但仔细想想还挺有意思的.简单的说就是增加事 ...
- hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏
use fgets, and remove the potential '\n' in the string's last postion. (main point) remove redundanc ...
- C++用法的学习心得(要求包含示例,并反映出利用网络获取帮助的过程)
大一一年C++的学习生涯,让我感慨颇多!回想起,当初上课时的情形,一切是那么的清晰,仿佛就像是发生在昨天一样. 任何一门学科的学习都是有技巧的.对于c++,我学的并不好,刚开 ...
- 启动Mysql服务提示Can’t connect to local MySQL server through socket的解决方法
启动Mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/ ...
- iOS7下隐藏status bar的详细研究
info.plist文件中,View controller-based status bar appearance项设为YES,则View controller对status bar的设置优先级高于a ...
- Hadoop安装——如何修改端口冲突
在一个集群中,尽管是多用户,但是端口是公用的,存在冲突的可能.如果另一个用户已经采用默认配置安装了hadoop,那么当前用户再安装hadoop时,必然会产生端口的冲突.在配置自己的hadoop时,可以 ...
- 在web.config配置中添加xml内容
在web.config 中添加需要的内容时, 就是在<configuration>节点内添加一个新的<configSections>元素, 在configSections元素中 ...