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 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 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
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 more than 100 pockets.
Sample Input
1 1
*
3 5
@@*
@
@@*
1 8
@@**@*
5 5
**@
@@@
@*@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
这题切记不能用stl的队列!!!!!!!!被坑死了!!!!!!!
会超内存的……
本题倒是简单,我用的广搜,碰到*全部变成X,遇到@就记录一次,然后同一个人矿井全部变成X,凡是全部变成X以后一次广搜就会结束…就这样结束就可以了
#include<iostream>
#include<queue>
#include<stack>
#include<string>
#include<vector>
using namespace std;
char tu[101][101];
struct point
{
int m;
int n;
};
point qwe[10000];
void bfs(point a,int m,int n)
{
int head=0,tail=0;
qwe[head].m= a.m;
qwe[head].n = a.n;
tail++;
while (tail != head)
{
point e = qwe[head];
tu[e.m][e.n] = 'X';
if(head!=9999)head++;
else head = 0;
for (int w = e.m - 1;w <= e.m + 1;w++)
{
if (w < 0|| w>m - 1)continue;
for (int y = e.n - 1;y <= e.n + 1;y++)
{
if (y<0 || y>n - 1)continue;
if (tu[w][y] == '@')
{
tu[w][y] = 'X';
qwe[tail].m = w;
qwe[tail].n = y;
if (tail != 9999)tail++;
else tail =0;
}
}
}
}
}
int main()
{
int ww, ee;
while (cin >> ww >> ee)
{
const int m = ww;
const int n = ee;
if (m == 0 && n == 0)break;
for (int a = 0;a < m;a++)
{
for (int b = 0;b < n;b++)
{
cin >> tu[a][b];
}
}
int w = 0;
for (int a = 0;a < m;a++)
{
for (int b = 0;b < n;b++)
{
if (tu[a][b] == '@')
{
point jieru = { a,b };
w++;
bfs(jieru,m,n);
}
if (tu[a][b] == '*')
{
tu[a][b] = 'X';
}
}
}
cout << w << endl;
}
return 0;
}