Oil Deposits

来源:互联网 发布:2016三毛淘宝小号网址 编辑:程序博客网 时间:2024/05/18 14:14

Problem 11 » Oil Deposits 查看标程

Description

TheGeoSurvComp geologic survey company is responsible for detectingunderground oil deposits. GeoSurvComp works with one largerectangular region of land at a time, and creates a grid thatdivides the land into numerous square plots. It then analyzes eachplot separately, using sensing equipment to determine whether ornot the plot contains oil. A plot containing oil is called apocket. If two pockets are adjacent, then they are part of the sameoil deposit. Oil deposits can be quite large and may containnumerous pockets. Your job is to determine how many different oildeposits are contained in a grid.

INPUT

Theinput file contains one or more grids. Each grid begins with a linecontaining 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 theinput; otherwise 1 <= m <= 100 and 1<= n <= 100. 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 an oilpocket.

OUTPUT

Foreach grid, output the number of distinct oil deposits. Twodifferent pockets are part of the same oil deposit if they areadjacent horizontally, vertically, or diagonally. An oil depositwill not contain more than 100 pockets.

SAMPLE INPUT

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

SAMPLE OUTPUT

0122

HINT

#include<iostream>using namespace std;int num,w,h;char a[105][105];void dfs(int x,int y){         if(x<0||x>=h||y<0||y>=w)                return;    if(a[x][y]=='*')                return ;        a[x][y]='*';    dfs(x-1, y-1);    dfs(x-1, y);    dfs(x-1, y+1);    dfs(x, y-1);    dfs(x, y+1);    dfs(x+1,y-1);    dfs(x+1,y);    dfs(x+1,y+1);}int main(){    int i,j;        while(cin>>h>>w&&w!=0&&h!=0)        {         for(i=0;i<h;i++)                 for(j=0;j<w;j++)                 cin>>a[i][j];         num=0;         for(i=0;i<h;i++)                 for(j=0;j<w;j++)                         if(a[i][j]=='@')                         {                                  dfs(i,j);                                 num++;                         }         cout<<num<<endl;        }}

 

 

0 0
原创粉丝点击