就是在输入的时候把 ‘o’ 的放在队里,然后,直接BFS就可以了。感觉是水题。
#include#include using namespace std;#define pa pair const int maxn = 1e3 + 10;char cc[maxn][maxn];int xx[] = { 0, 1, -1, 0, 0 };int yy[] = { 0, 0, 0, 1, -1 };char kk[] = " udlr";int n, m, ans;queue q;int main(){ cin >> n >> m; for (int i = 1; i <= n; ++i){ for (int j = 1; j <= m; ++j) { cin >> cc[i][j]; if (cc[i][j] == 'o'){ pair ss; ss.first = i; ss.second = j; q.push(ss); } } } while (q.size()){ pair ss = q.front(); q.pop(); ans++; for (int i = 1; i <= 4; ++i){ int x = ss.first + xx[i], y = ss.second + yy[i]; if ((x >= 1 && x <= n&&y >= 1 && y <= m) && cc[x][y] == kk[i]){ pair h; h.first = x; h.second = y; q.push(h); } } } cout << ans << endl;}