您的位置:首页 > 其它

Uva-572 - Oil Deposits(DFS)

2015-09-01 17:19 459 查看

  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 squareplots. 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 oildeposits 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  and .Following this are mlines of n characters each (not counting the end-of-linecharacters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing anoil 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 morethan 100 pockets.

Sample Input 

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

Sample Output 

0
1
2
2
#include <cstdio>#include <cstring>using namespace std;#define BG 105struct point{char c;int st;};point square[BG][BG];int m,n;void set_p(int i,int j){if(square[i][j].c=='@'&&square[i][j].st==0){square[i][j].st=1;if(i+1<m)//don't forget the diagonal{set_p(i+1,j);if(j+1<n)set_p(i+1,j+1);if(j-1>=0)set_p(i+1,j-1);}if(i-1>=0){set_p(i-1,j);if(j+1<n)set_p(i-1,j+1);if(j-1>=0)set_p(i-1,j-1);}if(j+1<n)set_p(i,j+1);if(j-1>=0)//8 types of directions in totalset_p(i,j-1);}}int main(){while(scanf("%d%d",&m,&n)==2&&m!=0){getchar();int sum=0;memset(square,0,sizeof(point)*BG*BG);int i,j;for(i=0;i<m;i++){for(j=0;j<n;j++)scanf("%c",&square[i][j].c);getchar();}for(i=0;i<m;i++)for(j=0;j<n;j++)if(square[i][j].c=='@'&&square[i][j].st==0){sum++;set_p(i,j);}printf("%d\n",sum);}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: