// uva 572 - Oil Deposits
// 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=513
// 八方向用广搜,求连通的块数
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int row, column;
int count;
char map[200][200];
int flag[200][200];
struct Node{
int x, y;
};
queue<Node> que;
Node now_node;
bool inmap(Node node){
return node.x >= 0 && node.x < row && node.y >= 0 && node.y < column;
}
int direct[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
void bfs();
int main(){
while (scanf("%d %d", &row, &column), row || column){
getchar();
memset(map, 0, sizeof(map));
memset(flag, 0, sizeof(flag));
count = 0;
for(int i = 0; i < row; i++) {
gets(map[i]);
}
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
if(map[i][j] == '@' && !flag[i][j]){
flag[i][j] = ++count;
now_node.x = i;
now_node.y = j;
bfs();
}
}
}
cout << count << endl;
}
return 0;
}
void bfs(){
que.push(now_node);
Node tmp_node;
while(que.size()){
now_node = que.front();
que.pop();
for(int i = 0; i < 8; i++){
tmp_node.x = now_node.x + direct[i][0];
tmp_node.y = now_node.y + direct[i][1];
if(inmap(tmp_node) && map[tmp_node.x][tmp_node.y] == '@' && !flag[tmp_node.x][tmp_node.y]){
flag[tmp_node.x][tmp_node.y] = count;
que.push(tmp_node);
}
}
}
}
uva 572 - Oil Deposits
最新推荐文章于 2021-05-26 22:19:09 发布