[HDU](1241)Oil Deposits ---dfs
来源:互联网 发布:java小球下落 编辑:程序博客网 时间:2024/05/29 12:20
Problem 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
解题新知:题意,就是找有几个由“@”构成的连通块。
思路,用dfs找到“@”将其变为“*”计数+1,然后将其周围8个方向相邻所有的“@”也变成“*”
AC代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char mmap[105][105];int vis[105][105];int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};int n,m;int ans;void dfs(int xx,int yy){ for(int i=0;i<8;i++) { int x = xx+dir[i][0]; int y = yy+dir[i][1]; if(mmap[x][y] == '@' && x>=0 && x<n && y>=0 && y<m) { mmap[x][y] = '*'; dfs(x,y); } }}int main(){ while(~scanf("%d %d",&n,&m)&&n&&m) { ans = 0; memset(mmap,0,sizeof(mmap)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cin>>mmap[i][j]; } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(mmap[i][j] == '@') { ans++; mmap[i][j] = '*'; dfs(i,j); } } } printf("%d\n",ans); } return 0;}
- hdu 1241 Oil Deposits (dfs)
- HDU-1241 Oil Deposits (DFS)
- HDU 1241 Oil Deposits (DFS)
- 【DFS】hdu:1241 Oil Deposits
- hdu 1241dfs Oil Deposits
- hdu 1241 Oil Deposits(DFS)
- HDU--1241:Oil Deposits (DFS)
- HDU 1241 Oil Deposits 【DFS】
- HDU 1241 Oil Deposits(dfs)
- HDU 1241 -- Oil Deposits (dfs)
- HDU 1241--Oil Deposits【DFS】
- HDU 1241 Oil Deposits(DFS)
- [dfs]HDU 1241 Oil Deposits
- HDU 1241 Oil Deposits DFS
- DFS-HDU-1241-Oil Deposits
- hdu 1241 Oil Deposits(dfs)
- hdu 1241 Oil Deposits(DFS)
- HDU-1241-Oil Deposits【DFS】
- Android事件处理之View$dispatchTouchEvent(ev)
- hdu 1285 确定比赛名次(拓扑排序)
- Service面试题目整理
- 百度2017年计算机视觉笔试题
- 观察者模式
- [HDU](1241)Oil Deposits ---dfs
- cout的输出流顺序
- 稀疏表示
- jqObj.eq(num)和jqObj.get(num)的区别
- 太上感应篇0024
- 容错
- 折半枚举(双向搜索) poj 4 Values whose Sum is 0
- MySQL索引的数据结构 -- 转载学习
- win10 安装NET Framework 3.5