Oil Deposits hd 1241
2016-07-28 20:59
288 查看
Description
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.
Input
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.
Output
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.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
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.
Input
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.
Output
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.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
#include<cstdio> #include<cstring> #include<iostream> using namespace std; char a[110][110]; int n,m,ans; int nx[4]={-1,1,0,0}; int ny[4]={0,0,-1,1}; void dfs(int r,int c) { if(r<0||r>=m||c<0||c>=n||a[r][c]!='@') return; else { a[r][c] = '!'; for(int d=0;d<4;d++) for(int f=0;f<4;f++) dfs(r+nx[d],c+ny[f]); } } int main() { int i,j; while(cin>>m>>n)<span style="white-space:pre"> </span>//不能用scanf输入 { if(m==0 || n==0) break; ans=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(a[i][j]=='@') { dfs(i,j); ans++; } } } cout<<ans<<endl; } return 0; }
相关文章推荐
- Java 理论与实践: 流行的原子
- 什么是SCRUM?
- 什么是SCRUM?
- Avoid The Lakes(DFS)
- spark error
- 超强PCB布线设计经验谈附原理图
- 银河英雄传说(codevs 1540)
- JS+DIV 实现拖动效果
- JavaScript清空、重置form表单
- POJ 3170--双重BFS
- 安卓手把手教你实现集成支付宝支付功能
- 拦截api
- linux 文件系统详解
- 二维码
- 变形课 hd 1181
- 真机Win7 64位,虚拟机MAC OS X 10.11用NAT无法上网问题,ip 169.254.xxx.xxx
- 纯命令提交代码到git仓库(教你怎么装逼)
- 纯命令提交代码到git仓库(教你怎么装逼)
- 纯命令提交代码到git仓库(教你怎么装逼)
- JavaScript 刷新/重新加载当前页面