首页 > 代码库 > hdu 1241 Oil Deposits
hdu 1241 Oil Deposits
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13050 Accepted Submission(s): 7560
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
Source
Mid-Central USA 1997
PS:第一次提交就AC了,算是小欣慰吧
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdlib> 6 using namespace std; 7 int vis[103][103]; 8 char map[103][103]; 9 int n,m;10 int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};11 //方向左上,上,右上,左,右,左下,下,右下12 13 14 void dfs(int x,int y)15 {16 int ss,vv,i;17 for(i=0;i<8;i++)18 {19 ss=x+dir[i][0];20 vv=y+dir[i][1];21 if(ss>=0&&ss<n && vv>=0&&vv<m &&map[ss][vv]==‘@‘ && !vis[ss][vv])22 {23 vis[ss][vv]=1;24 dfs(ss,vv);25 }26 }27 return ;28 }29 30 int main()31 {32 //freopen("in.txt","r",stdin);33 while(~scanf("%d%d",&n,&m))34 {35 memset(vis,0,sizeof(vis));36 int i,j;37 int sum=0;38 if(!n&&!m)39 break;40 for(i=0;i<n;i++)41 scanf("%s",map[i]);42 for(i=0;i<n;i++)43 {44 for(j=0;j<m;j++)45 {46 if(!vis[i][j] && map[i][j]!=‘*‘)47 {48 dfs(i,j);49 sum++;50 }51 }52 }53 printf("%d\n",sum);54 }55 return 0;56 }
hdu 1241 Oil Deposits
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。