题意:求有几个油田,八个方向有一个方向是相同的就是联通的,也就是一个。
直接bfs
跟图的遍历差不多
遍历后把@改为*(判重)
#include
#include
#include
#include
using namespace std;
const int N = 1000;
int n, m;
char str[N][N];
struct node
{
int x, y;
node(){}
node(int xx, int yy)
{
x = xx; y = yy;
}
};
queue
que;
int dir[9][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1} };
int bfs(int x, int y)
{
while(!que.empty()) que.pop();
que.push( node(x, y) );
while(!que.empty())
{
node tmp = que.front(); que.pop();
int xx, yy;
for(int i = 0; i < 8; i++)
{
xx = tmp.x + dir[i][0];
yy = tmp.y + dir[i][1];
if(0 <= x && x < n && 0 <= yy && yy < m && str[xx][yy] == '@')
{
str[xx][yy] = '*';
que.push( node(xx, yy) );
}
}
}
return 0;
}
int main(void)
{
while(scanf("%d%d", &n, &m), n+m)
{
int i, j;
for(i = 0; i < n; i++)
scanf("%s", str[i]);
int ans = 0;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
if(str[i][j] == '@')
{
bfs(i, j);
ans++;
}
}
printf("%d\n", ans);
}
return 0;
}