标签:
# include <iostream> # include <fstream> # include <cstring> using namespace std; int n, m; char map[1001][1001]; int is[1001][1001]; int dir[8][2] = { {-1,0},{1, 0},{0, 1},{0, -1},{1, 1},{1, -1},{-1, 1},{-1, -1}}; bool border(int i, int j) { if(i >= 1 && i <= n && j <= m && j >= 1) return true; return false; } void bfs(int, int); int main() { //fstream cin("aaa.txt"); while(cin >> n >> m) { if(n == 0 && m == 0) break; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> map[i][j]; memset(is, 0, sizeof(is)); int jishu = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { if(map[i][j] == ‘@‘ && is[i][j] == 0) { is[i][j] = 1; jishu++; bfs(i, j); } } cout << jishu << endl; } return 0; } void bfs(int a, int b) { int x, y; for(int i = 0; i < 8; i++) { x = a + dir[i][0]; y = b + dir[i][1]; if(border(x, y) && map[x][y] == ‘@‘ && is[x][y] == 0) { is[x][y] = 1; bfs(x, y); } } }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5399341.html