首页 > 代码库 > Oil Deposits

Oil Deposits

1533: Oil Deposits

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 477  Solved: 303

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
不能在原有基础上直接处理传递的参数x,y.要在设两个参数x1,y1.
技术分享
 1 #include<stdio.h> 2 int r,c; 3 char oil[101][101]; 4 int dir[][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{-1,1},{1,-1}}; 5  6 void dfs(int x,int y) 7 { 8     int i; 9     int x1,y1;10     oil[x][y]=*;11     for(i=0;i<8;i++)12     {13         x1=x+dir[i][0];14         y1=y+dir[i][1];15 16         if(x1<0||y1<0||x1>=r||y1>=c)17             continue;18         if(oil[x1][y1]==*)19             continue;20        // if(oil[x][y]==‘@‘)21             dfs(x1,y1);22     }23 }24 /*25 void solve(int i,int j)26 {27     int x,y,k;28     oil[i][j]=‘*‘;29     for(k=0;k<8;k++)30     {31        x=i+dir[k][0];32        y=j+dir[k][1];33        if(x<0||x>=r||y<0||y>=c||oil[x][y]==‘*‘)34          continue;35        solve(x,y);36     }37 }*/38 int main()39 {40   //  freopen("a.txt","r",stdin);41     int i,j;42     int cnt;43     while(scanf("%d%d",&r,&c)!=EOF,r&&c)44     {45         cnt=0;46         for(i=0;i<r;i++)47             scanf("%s",oil[i]);48         for(i=0;i<r;i++)49         {50             for(j=0;j<c;j++)51             {52                 if(oil[i][j]==@)53                 {54                     cnt++;55                     dfs(i,j);56                 }57             }58         }59         printf("%d\n",cnt);60     }61     return 0;62 }
dfs

 

Oil Deposits