hdu 1241

复制代码
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 48366    Accepted Submission(s): 27816


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
 
复制代码

 

 

复制代码
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <string>
 5 #include <string>
 6 #include <cstring>
 7 #include <map>
 8 #include <utility>
 9 using namespace std;
10 const int  N = 1e2+20 ;
11 int m,n;
12 char s[N][N];
13 int flag,ans;//flag定义为全局变量 
14 int dir[8][2]={
15 {0,1},{0,-1},{1,0},{-1,0},
16 {-1,1},{1,1},{-1,-1},{1,-1}
17 };
18 bool check(int r,int l){
19     if(r>=0&&r<m&&l>=0&&l<n){
20         return true;
21     }
22     return false;
23 }
24 bool   dfs(int  r,int l){
25     if(s[r][l]=='*') return flag;
26     flag=1;
27     s[r][l]='*';//避免重复 
28     for(int i=0;i<8;i++){
29             int ii=r+dir[i][0];
30             int jj=l+dir[i][1];
31             if(check(ii,jj)){
32                 dfs(ii,jj);//把和这个石油是同一种的全变为* 
33             }
34     }
35     return flag;//一定要return  
36 }
37 int  main()
38 {
39     while(~scanf("%d%d",&m,&n))
40     {
41         if(n==0&&m==0) break;
42         for(int i=0;i<m;i++)
43         scanf("%s",s[i]);
44         ans=0;
45         for(int i=0;i<m;i++){
46             for(int j=0;j<n;j++){
47                 flag=0;
48                 if(dfs(i,j)){//找到一种石油 
49                     ans++;
50                 }
51             }
52         }
53             printf("%d\n",ans);
54     }
55   return  0;
56 }
复制代码

 

posted on   cltt  阅读(76)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示