ZOJ 1709
主要运用BFS把一个’@‘所在的油田的‘@’都标记(既改为‘*’),
然后不断的找’@‘,重复刚才那一步,直到找不到’@‘为止;记录找的次数;
#include <iostream>
#include <queue>
#include "string.h"
using namespace std;
struct spot{
public:
int x, y;
void init(int nx, int ny){
x = nx;
y = ny;
}
};
int n = 0, m = 0;
char oil[101][101];
int step[8][2] = {{1, 1}, { -1, 1}, { -1, -1}, {1, -1}, {1, 0}, {0, 1}, { -1, 0}, {0, -1}}; //八个方向
void bfs( int x, int y){
int nx,ny;
queue<spot> q;
spot start;
start.init(x, y);
q.push(start);
oil[y][x]=1;
while (!q.empty()){
spot t = q.front();
q.pop();
int x = t.x, y = t.y;
for (int i = 0; i < 8; i++){
nx = x + step[i][0];
ny = y + step[i][1];
if(nx >= 0 && ny >= 0 && nx < m && ny < n && oil[ny][nx] == '@'){
spot c;
c.init(nx, ny);
q.push(c);
oil[ny][nx] = '*';
}
}
}
}
int main()
{
while (cin>>n>>m){
if (m == 0)break;
int sum = 0;
memset(oil, 0, sizeof(oil));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin>>oil[i][j];
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (oil[i][j]=='@'){
sum ++;
bfs(j, i);
}
}
}
cout << sum << endl;
}
return 0;
}