标签:
# include <iostream> # include <queue> # include <cstring> # include <fstream> using namespace std; // 好坑呀, 先输入列 在输入行 struct Info { int x; int y; }start; int n, m; char map[101][101]; int is[101][101]; int dir[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int dfs(); bool border(Info & info) { if(info.x >= 1 && info.x <= m && info.y >= 1 && info.y <= n) return true; return false; } int main() { //fstream cin("aaa.txt"); while(cin >> n >> m) { if(n == 0 && m == 0) break; memset(is, 0, sizeof(is)); for(int i = 1 ; i <= m; i++) for(int j = 1; j <= n; j++) { cin >> map[i][j]; if(map[i][j] == ‘@‘) { start.x = i; start.y = j; } } is[start.x][start.y] = 1; map[start.x][start.y] = ‘.‘; cout << dfs() << endl; } return 0; } int dfs() { int jishu = 1; queue <Info> Q; Q.push(start); Info now, next; while(!Q.empty()) { now = Q.front(); Q.pop(); for(int i = 0; i < 4; i++) { next.x = now.x + dir[i][0]; next.y = now.y + dir[i][1]; if(!border(next)) continue; if(map[next.x][next.y] == ‘#‘) continue; if(is[next.x][next.y]) continue; jishu++; is[next.x][next.y] = 1; Q.push(next); } } return jishu; }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5401935.html