HDU 1241(DFS)
来源:互联网 发布:网络技术服务工程师 编辑:程序博客网 时间:2024/05/16 14:59
问题描述:
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 0Sample Output
0122
题目题意:问我们@的联通块有多少个
题目分析:DFS,找到一个@就搜下去,把找到的@变成*,搜完全图。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;const int maxn=105;char mmap[105][105];int Next[8][2]={{-1,0},{1,0},{0,1},{0,-1},{-1,1},{1,1},{-1,-1},{1,-1}};//8个方向int n,m,ans;void dfs(int x,int y){ if (mmap[x][y]=='@') mmap[x][y]='*'; for (int i=0;i<8;i++) { int dx=x+Next[i][0]; int dy=y+Next[i][1]; if (dx<0||dx>n||dy<0||dy>m||mmap[dx][dy]=='*') continue; if (mmap[dx][dy]=='@') { dfs(dx,dy); } }}int main(){ while (scanf("%d%d",&n,&m)!=EOF) { if (n==0&&m==0) break; for (int i=0;i<n;i++) scanf("%s",mmap[i]); ans=0; for (int i=0;i<n;i++) { for (int j=0;j<m;j++) { if (mmap[i][j]=='@') { dfs(i,j); ans++; } } } printf("%d\n",ans); } return 0;}
- HDU 1241 DFS
- hdu 1241 DFS
- hdu 1241 经典DFS
- hdu 1241 (dfs)
- HDU 1241 DFS
- hdu 1241(dfs)
- hdu 1241 简单dfs
- hdu 1241--入门DFS
- hdu 1241 dfs染色
- hdu 1241 dfs
- HDU 1241 DFS--分块
- HDU 1241 DFS算法
- 图论 DFS HDU 1241
- HDU 1241(DFS)
- HDU 1241 DFS经典
- -----hdu 1241 dfs
- HDU--1241 简单DFS
- HDU 1241(DFS)
- 关于QT中遇到的问题
- 定时删除edas多余日志
- Kotlin总结3
- 实现RecyclerView类似3D效果的消失
- [Error Code]:RequestTimeTooSkewed, [Message]:The difference between the request time and the current
- HDU 1241(DFS)
- OpenGL学习: 光照系列1-光照基础(phong模型)
- Linux bash 之declare
- POJ-2139 Six Degrees of Cowvin Bacon
- 57
- 西游记思维导图大全
- leetcode 227. Basic Calculator II 字符串表达式计算
- 对list中的实体类或map重新排序
- Ubuntu异常关机后无法启动图形界面,提示 Welcome to emergency mode...的解决方法