hdu 1241 Oil Deposits (DFS)(BFS)
来源:互联网 发布:蒋欣刘涛关系破裂知乎 编辑:程序博客网 时间:2024/06/07 22:54
DFS代码#include<iostream>#include<cstring> using namespace std;char map[102][102];int book[109][106];int dire[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};int row,col,cnt;void dfs(int x,int y){for(int i=0;i<8;i++){int tx=x+dire[i][0];int ty=y+dire[i][1];if(tx<1||tx>row||ty>col||ty<1)continue;if(!book[tx][ty]&&map[tx][ty]=='@'){book[tx][ty]=1;dfs(tx,ty); }}return ;}int main(){while(cin>>row>>col,row){memset(book,0,sizeof(book));cnt=0;for(int i=1;i<=row;i++)for(int j=1;j<=col;j++){cin>>map[i][j]; } for(int i=1;i<=row;i++)for(int j=1;j<=col;j++){if(!book[i][j]&&map[i][j]=='@'){book[i][j]=1;dfs(i,j);cnt++;} } cout<<cnt<<endl;}return 0; }
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
0122
BFS代码:#include<iostream>#include<cstring>#include<queue>using namespace std;char map[120][120];int m,n;int book[120][120];int dire[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};struct point{int x;int y; point(int a,int b) { x=a;y=b; }};queue<point> q;int cnt;void bfs(point p){book[p.x][p.y]=1;q.push(p);while(!q.empty()){point tmp=q.front();q.pop();for(int i=0;i<8;i++){int tx=tmp.x+dire[i][0];int ty=tmp.y+dire[i][1];if(tx>m||tx<1||ty<1||ty>n)continue;if(map[tx][ty]=='@'&&!book[tx][ty]){book[tx][ty]=1;q.push(point(tx,ty));} } }}int main(){while(cin>>m>>n,m+n){memset(book,0,sizeof(book));cnt=0;for(int i=1;i<=m;i++)for(int j=1;j<=n;j++){cin>>map[i][j];}for(int i=1;i<=m;i++)for(int j=1;j<=n;j++){if(!book[i][j]&&map[i][j]=='@') { bfs(point(i,j)); cnt++; } } cout<<cnt<<endl;}return 0; }DFS代码#include<iostream>#include<cstring> using namespace std;char map[102][102];int book[109][106];int dire[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};int row,col,cnt;void dfs(int x,int y){for(int i=0;i<8;i++){int tx=x+dire[i][0];int ty=y+dire[i][1];if(tx<1||tx>row||ty>col||ty<1)continue;if(!book[tx][ty]&&map[tx][ty]=='@'){book[tx][ty]=1;dfs(tx,ty); }}return ;}int main(){while(cin>>row>>col,row){memset(book,0,sizeof(book));cnt=0;for(int i=1;i<=row;i++)for(int j=1;j<=col;j++){cin>>map[i][j]; } for(int i=1;i<=row;i++)for(int j=1;j<=col;j++){if(!book[i][j]&&map[i][j]=='@'){book[i][j]=1;dfs(i,j);cnt++;} } cout<<cnt<<endl;}return 0; }
0 0
- HDU-#1241 Oil Deposits(BFS & DFS)
- HDU 1241 Oil Deposits[dfs || bfs]
- HDU 1241 Oil Deposits (DFS/BFS)
- HDU 1241 Oil Deposits (DFS+BFS)
- HDU 1241/UVa572 Oil Deposits(DFS&BFS)
- hdu 1241 Oil Deposits (DFS)(BFS)
- 【hdu 1241】 Oil Deposits (dfs+bfs)
- HDU 1241 Oil Deposits (基础DFS 或者 BFS)
- 【HDU】-1241-Oil Deposits(DFS || BFS&&队列)
- HDU-Oil Deposits (DFS,BFS,水题)
- hdu 1241 Oil Deposits - bfs
- hdu 1241Oil Deposits(BFS)
- hdu 1241 Oil Deposits (BFS)
- HDU 1241 BFS-Oil Deposits
- 【BFS】HDU 1241 Oil Deposits
- HDU 1241 Oil Deposits <BFS>
- Oil Deposits BFS+DFS
- Oil Deposits(dfs/bfs)
- 辅lua
- Android热补丁动态修复技术(一):从Dex分包原理到热补丁
- hdf5和las文件的读写
- IOS中的GCD的简要认识
- Android SVG动画
- hdu 1241 Oil Deposits (DFS)(BFS)
- Android控件CoordinatorLayout和TabLayout
- viewpagerIndcator 的使用步骤
- 学习笔记 新浪微博篇 问题篇一 文件不存在c8998
- SQL:MINUS 用法
- 解决Eclipse每次修改完代码后需要先Clean,不然部署不上文件的问题
- OpenCV Scalar详解
- 本地多级文件 原样上传到hdfs
- 利用scp 远程上传下载文件/文件夹