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
PS:第一次提交就AC了,算是小欣慰吧
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步