1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include <iostream> #include <queue> #include <cstring> using namespace std;
const int len = 300; char Map[len][len]; int vis[len][len] = {0}; int ans = 0; int w, h;
struct point { int x, y; }in, out, beg; queue<point> q;
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
int check(int x, int y) { if(!vis[x][y] && x >= 1 && y >= 1 && x <= h && y <= w && Map[x][y] != '#') return 1; return 0; }
void bfs(int x, int y) { vis[beg.x][beg.y] = 1; q.push(beg); while(!q.empty()) { out = q.front(); q.pop(); for(int i = 0; i < 4; i++) { in.x = out.x + dir[i][0]; in.y = out.y + dir[i][1]; if(check(in.x, in.y)) { q.push(in); vis[in.x][in.y] = 1; ans++; } } } }
int main() { ios::sync_with_stdio(false); cin >> w >> h; for(int i = 1; i <= h; i++) for(int j = 1; j <= w; j++) { cin >> Map[i][j]; if(Map[i][j] == '@') { beg.x = i; beg.y = j; } } bfs(beg.x, beg.y); cout << ans + 1; return 0; }
|