HDU 1312 Red and Black 题解

发布时间 2023-07-30 09:52:56作者: Miya555
//注意边界判断,调了好久

#include <iostream> #include <queue> using namespace std; #define check(x,y)(x<wx&&x>=0&&y<hy&&y>=0) struct node { int x, y; }; char room[23][23]; int n, m, wx, hy, num; int dir[4][2] = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} }; void bfs(int dx, int dy) { num = 1; queue<node>q; node start, next; start.x = dx; start.y = dy; q.push(start); while (!q.empty()) { start = q.front(); q.pop(); // cout<<"out"<<start.x<<' '<<start.y<<endl; for (int i = 0; i < 4; i++) { next.x = start.x + dir[i][0]; next.y = start.y + dir[i][1]; if (check(next.x, next.y) && room[next.x][next.y] == '.') { room[next.x][next.y] = '#'; num++; q.push(next); } } } } int main() { int x, y, dx, dy; while (cin >> wx >> hy) { if (wx == 0 && hy == 0) { break; } for ( y = 0; y < hy; y++) { for (int x = 0; x < wx; x++) { cin >> room[x][y]; if (room[x][y] == '@') { dx = x; dy = y; } } } num = 0; bfs(dx, dy); cout << num << endl; } return 0; }