Oil Deposits

Oil Deposits

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
题解:
这题,和求有几个水坑的题很像,从搜索到第一个水洼开始,像各个方向搜索,并将其改成' * ',以防搜索重复,
然后每进行一次dfs就是找到一片新的水洼。
代码:
 #include <stdio.h>
 #include <algorithm>
 #include <math.h>
 #include <set>
 #include <queue>
 #include <stack>
 #include <map>
 #include <string.h>
 #include <iostream>
 #include <vector>
 
 using namespace std;
 
 int m,n;
 int ex,ey;
 char a[105][105];
 int vis[105][105];
 int d[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}};
 int ans;
 
 void DFS(int x,int y)
 {    
     if(x<0||y<0||x>=m||y>=n)
         return ;
     for(int i=0;i<8;i++)
     {
         int dx=x+d[i][0];
         int dy=y+d[i][1];
         if(dx>=0&&dy>=0&&dx<m&&dy<n&&a[dx][dy]!='*')
         {  a[dx][dy]='*';
             DFS(dx,dy);
         }
     }
 }
 int main()
 {    while(cin>>m>>n&&m)    {    ans=0;    for(int i=0;i<m;i++)    {    for(int j=0;j<n;j++)    {    cin>>a[i][j];    }    }    for(int i=0;i<m;i++)    {  for(int j=0;j<n;j++)    {  if(a[i][j]=='@')    {    ans++;     ex=i;
                        ey=j;
                        a[ex][ey]='*';     DFS(ex,ey);  }  }  }   cout<<ans<<endl;  }  return 0;
 }
 
 

全部评论

相关推荐

微风不断:兄弟,你把四旋翼都做出来了那个挺难的吧
点赞 评论 收藏
分享
11-08 17:36
诺瓦科技_HR
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务