5分求解
查看原帖
5分求解
1252300
chi_li_zi楼主2024/7/21 12:52
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};

int n, m, q;
vector<string> maze;
vector<vector<bool>> canReach;

bool inBounds(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m;
}

void bfs() {
    queue<pair<int, int>> que;
    canReach.assign(n, vector<bool>(m, false));
    canReach[0][0] = true;
    que.push({0, 0});

    while (!que.empty()) {
        int x = que.front().first;
        int y = que.front().second;
        que.pop();

        for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (inBounds(nx, ny) && !canReach[nx][ny] && maze[nx][ny] == '.') {
                canReach[nx][ny] = true;
                que.push({nx, ny});
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;
    maze.resize(n);

    for (int i = 0; i < n; ++i) {
        cin >> maze[i];
    }

    bfs();

    cin >> q;

    while (q--) {
        long long r, c;
        cin >> r >> c;

        int x = (r % n + n) % n;
        int y = (c % m + m) % m;

        if (maze[x][y] == '#' || !canReach[x][y]) {
            cout << "no" << endl;
        } else {
            cout << "yes" << endl;
        }
    }

    return 0;
}
2024/7/21 12:52
加载中...