HDU 1141 入门DFS
来源:互联网 发布:linux rpm包安装位置 编辑:程序博客网 时间:2024/05/17 06:19
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33940 Accepted Submission(s): 19735
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
Source
Mid-Central USA 1997
Recommend
Eddy
#include<stdio.h>#include<string.h>char map[105][105];int num;int n,m;int dir[][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};void dfs(int x,int y){if(map[x][y]=='*')return ;map[x][y]='*';for(int i=0;i<8;i++){int nx=x+dir[i][0];int ny=y+dir[i][1];if(nx>=1 && nx<=n && ny>=1 && ny<=m)dfs(nx,ny);}return ;}int main(){while(scanf("%d%d",&n,&m) && n+m){num=0;for(int i=1;i<=n;i++)scanf("%s",&map[i][1]);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(map[i][j]=='@'){num++;dfs(i,j); }printf("%d\n",num);}return 0;}
#include<stdio.h>#include<string.h>int dir[][2]={0,1,0,-1,-1,0,1,0,1,1,1,-1,-1,-1,-1,1};char map[105][105];int book[105][105];int a,b;void dfs(int x,int y){int nx,ny;if(map[x][y]=='*' || book[x][y]==1)return ;book[x][y]=1;for(int i=0;i<8;i++){nx=x+dir[i][0];ny=y+dir[i][1];if(nx>=1 && ny>=1 && nx<=a && ny<=b)dfs(nx,ny);}return ;}int main(){int i,j,res;while(scanf("%d%d\n",&a,&b) , a+b){res=0;memset(book,0,sizeof(book));for(i=1;i<=a;i++){for(j=1;j<=b;j++){scanf("%c",&map[i][j]);}getchar();}for(i=1;i<=a;i++){for(j=1;j<=b;j++){if(book[i][j]==0 && map[i][j]=='@'){res++;dfs(i,j);}}}printf("%d\n",res);}}
阅读全文
0 0
- HDU 1141 入门DFS
- hdu 1241--入门DFS
- HDU 1242 Rescue(DFS入门)
- HDU Safecracker 1015(dfs入门)
- HDU 1241 Oil Deposits DFS入门
- hdu 1241 Oil Deposits(dfs入门)
- hdu 1312 Red and Black(dfs入门)
- HDU 1312 Red and Black (DFS入门)
- hdu 1045 Fire Net DFS入门题
- #HDU 1312 Red and Black 【DFS入门】
- hdu 1312 BFS DFS搜索入门
- HDU 1016 Prime Ring Problem(DFS入门)
- hdu 1241 Oil Deposits (DFS入门)
- HDU 1016 Prime Ring Problem(DFS入门)
- HDU 2063 二分匹配入门 匈牙利算法DFS实现
- hdu 1241 Oil Deposits (dfs经典入门)
- HDU 1312 Red and Black DFS入门经典例题
- HDU 2553 N皇后问题 (简单DFS入门)
- 防火墙引起:SocketException: Software caused connection abort: recv failed
- Groovy基本使用(4):Java Groovy 相互调用
- dlib 13 dlib自带demo DNN狗脸检测
- 第三周【项目4
- TabLayout和ViewPager的联动效果实现
- HDU 1141 入门DFS
- 黑暗游侠--完成后的感想
- Groovy基本使用(5):文件I/O 处理
- Android 编码规范说明文档
- 10个基本Linux命令
- input,type="file"的样式修改
- C++之静态成员变量和静态成员函数
- Three.js 实现3D房间布局的简单实现
- Ngnix配置