Oil Deposits
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13505 | Accepted: 7345 |
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
0 1 2 2
Source
分析:
只有map[i][j]==0,该位置是新的oil deposit。然后广搜。
1 #include<iostream> 2 #include<queue> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 int m,n; 7 short map[105][105]; 8 short dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}}; 9 bool check(int x,int y){ 10 if(x<0||x>=m||y<0||y>=n||map[x][y]) 11 return false; 12 return true; 13 } 14 int main(){ 15 while(scanf("%d%d",&m,&n)&&m!=0&&n!=0){ 16 memset(map,0,sizeof(map)); 17 int i,j; 18 for(i=0;i<m;i++){ 19 for(j=0;j<n;j++){ 20 char c; 21 cin>>c; 22 if(c==‘*‘){ 23 map[i][j]=-1; 24 } 25 else{ 26 map[i][j]=0; 27 } 28 } 29 } 30 //cout<<1<<endl; 31 /*for(i=0;i<m;i++){ 32 for(j=0;j<n;j++){ 33 cout<<map[i][j]<<endl; 34 } 35 }*/ 36 int count=0; 37 for(i=0;i<m;i++){ 38 for(j=0;j<n;j++){ 39 if(map[i][j]==0){ 40 queue<int> q; 41 q.push(i*100+j); 42 map[i][j]=++count; 43 while(!q.empty()){ 44 short x=q.front()/100; 45 short y=q.front()%100; 46 q.pop(); 47 int k; 48 for(k=0;k<8;k++){ 49 short xx=x+dir[k][0]; 50 short yy=y+dir[k][1]; 51 if(check(xx,yy)){ 52 q.push(xx*100+yy); 53 map[xx][yy]=map[x][y]; 54 } 55 } 56 } 57 } 58 } 59 } 60 /*for(i=0;i<m;i++){ 61 for(j=0;j<n;j++){ 62 cout<<map[i][j]<<endl; 63 } 64 }*/ 65 cout<<count<<endl; 66 } 67 return 0; 68 }