POJ 1562 Oil Deposits

来源:互联网 发布:ip地址是阿里云 编辑:程序博客网 时间:2024/06/07 05:21
Oil Deposits
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 17335 Accepted: 9233

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 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

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

#include<stdio.h>   using namespace std;    const int MAX_N = 100 + 10;  char map[MAX_N][MAX_N];  int n, m, cnt;    //cnt为连通分量数目   void dfs(int i, int j)  {      if(i < 0 || i >= n || j < 0 || j >= m)          return;      if(map[i][j] != '@')          return;      if(map[i][j] == '@')      {          map[i][j] = '.';  //标记该点被走过         dfs(i - 1, j - 1);          dfs(i - 1, j);          dfs(i - 1, j + 1);          dfs(i, j - 1);          dfs(i, j + 1);          dfs(i + 1, j - 1);          dfs(i + 1, j);          dfs(i + 1, j + 1);      }  }  int main()  {      while(scanf("%d%d",&n,&m)&&m)      {          cnt = 0;           //for(int i = 0; i < n; i++)            //  scanf("%s",map[i]);         for(int i = 0; i < n; i++)         {         for(int j = 0; j < m; j++)         {         scanf("\n%c",&map[i][j]);   //一定不要少换行符  } }         for(int i = 0; i < n; i++)          {              for(int j = 0; j < m; j++)              {                  if(map[i][j] == '@')                  {                      cnt++;                         dfs(i,j);                  }              }          }          printf("%d\n",cnt);      }        return 0;  } #include<stdio.h>void dfs(int x,int y);char oil[100][100];bool visit[100][100];int m,n;//算符存在一个二维数组中 int go[8][2] = {{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,-1},{-1,0}};void search(int x,int y){int i;visit[x][y] = true;for(i = 0; i < 8; i++){if((x+go[i][0])>=0&&(x+go[i][0])<m&&(y+go[i][1])>=0&&(y+go[i][1])<n&&oil[x+go[i][0]][y+go[i][1]]=='@'&&!visit[x+go[i][0]][y+go[i][1]])//假如没有越界,并且是未搜索到的石油区域{search(x+go[i][0],y+go[i][1]);}}return ; }   int main() { int j; while(scanf("%d%d",&n,&m)&&m) { for(int i = 0; i < m; i++) { for(j = 0; j < n; j++) { scanf("%c",oil[i][j]); visit[i][j] = false; } }  int count = 0;  //计数值 for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { if(oil[i][j]=='@'&&!visit[i][j]) { seach(i,j); count++; } }  }   printf("%d\n",count); } return 0; }


0 0
原创粉丝点击