暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

时间:2023-03-09 13:33:43
暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

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 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

1
2
2
问题分析:依旧是用的bfs,虽然说dfs更好。。。。。,但是用bfs的话要注意让它搜完再进行下一次搜索,直到把所有油田走完,。
注意:此题描述的退出条件有误,应该是吗n>0才会退出,而且“An oil deposit will not contain more than 100 pockets.”这句描述无意义。
 #include "iostream"
#include "queue"
using namespace std;
struct person
{
int i;
int j;
};
char o[][];
void obegin(int n,int m)
{
int i,j;
for (i=;i<=n+;i++)
for (j=;j<=m+;j++)
{
if (i*j == || i == n+ || j == m+)
o[i][j] = '*';
else
cin>>o[i][j];
}
}
int dfs(int n,int m)
{
queue <person> p;
person fir,sec;
int c=;
int f;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
if (o[i][j] == '@')
{
f=;
fir.i = i;
fir.j = j;
c++;
p.push(fir);
while (!p.empty())
{
sec = p.front();
p.pop();
for (int k=;k<=;k++)
{
switch(k)
{
case : if (o[sec.i+][sec.j] == '@')
{
fir.i=sec.i+;
fir.j=sec.j;
}break;
case :if (o[sec.i-][sec.j] == '@')
{
fir.i=sec.i-;
fir.j=sec.j;
}break;
case :if (o[sec.i][sec.j+] == '@')
{
fir.i=sec.i;
fir.j=sec.j+;
}break;
case :if (o[sec.i][sec.j-] == '@')
{
fir.i=sec.i;
fir.j=sec.j-;
}break;
case :if (o[sec.i+][sec.j+] == '@')
{
fir.i=sec.i+;
fir.j=sec.j+;
}break;
case :if (o[sec.i+][sec.j-] == '@')
{
fir.i=sec.i+;
fir.j=sec.j-;
}break;
case :if (o[sec.i-][sec.j-] == '@')
{
fir.i=sec.i-;
fir.j=sec.j-;
}break;
case :if (o[sec.i-][sec.j+] == '@')
{
fir.i=sec.i-;
fir.j=sec.j+; }break;
}
if (o[fir.i][fir.j] == '@')
{
o[fir.i][fir.j]='#';
p.push(fir);
f++;
}
if (f/ == )
{
f=;
c++;
}
}
}
}
return c;
}
int main()
{
int n,m;
while (cin>>m>>n && n)
{
obegin(m,n);
cout<<dfs(m,n)<<endl;
}
return ;
}