标签:
#include<stdio.h> #include<string.h> #include<queue> #define MAX 107 using namespace std; int begin_x, begin_y; int n, m, count; char mapp[MAX][MAX]; int visited[MAX][MAX]; int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; struct node { int x; int y; }; int check(int x, int y) { if(x >= 0 && x < n && y >= 0 && y < m && mapp[x][y] != ‘#‘ && visited[x][y] == 0) return 1; return 0; } void bfs(int x, int y) { queue<node> q; struct node temp, next; temp.x = begin_x; temp.y = begin_y; q.push(temp); visited[temp.x][temp.y] = 1; while(!q.empty()) { temp = q.front(); q.pop(); count++; for(int i = 0; i < 4; i++) { next.x = temp.x + dir[i][0]; next.y = temp.y + dir[i][1]; if(check(next.x, next.y)) { q.push(next); visited[next.x][next.y] = 1; } } } } int main() { while(scanf("%d%d", &m, &n)!=EOF && (n || m)) { getchar(); count = 0; memset(visited, 0, sizeof(visited)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { scanf("%c", &mapp[i][j]); if(mapp[i][j] == ‘@‘) { begin_x = i; begin_y = j; } } getchar(); } bfs(begin_x, begin_y); printf("%d\n",count); } return 0; }
标签:
原文地址:http://www.cnblogs.com/burning-flame/p/5396587.html