codeforces D. Solve The Maze

codeforces D. Solve The Maze_第1张图片

题目

题意:

你有一个地图,总共有 4 4 4种符号,’.’,’#’,‘ G G G’,‘ B B B’,分别代表着路,墙,好人,坏人,现在你可以将路变成墙,问最后是否有一种方案使得坏人走不到 n , m n,m n,m好人可以。

思路:

n , m n,m n,m点开始 b f s bfs bfs,当到达某个点的时候,假如这个点旁边有坏人的话,那么这个点肯定要做墙,因为 b f s bfs bfs后,这个点是可到达 n , m n,m n,m的,所以这个肯定要当作墙,然后最后的时候将地图遍历一遍,如果这个点是好人并且是可到达的,那么就输出 y e s yes yes,否则就是 n o no no

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
int xxxxxxxxx = 1;
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
inline void debug(ll x) {cout << xxxxxxxxx++ << " " << x << endl;}
inline void debugs(string s) {cout << s << endl;}
const int maxn = 60;
struct NODE {
    int x;
    int y;
    NODE (int x, int y) : x(x), y(y) {}
};
int n, m;
int nextn[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
char mp[maxn][maxn] = {0};
bool book[maxn][maxn] = {false};
bool check(int x, int y) {
    if (x <= n && x - 1 >= 1 && mp[x - 1][y] == 'B') return false;
    if (x + 1 <= n && x >= 1 && mp[x + 1][y] == 'B') return false;
    if (y <= m && y - 1 >= 1 && mp[x][y - 1] == 'B') return false;
    if (y + 1 <= m && y >= 1 && mp[x][y + 1] == 'B') return false;
    return true;
}
void bfs(int sx, int sy) {
    queue<NODE> q;
    if (check(sx, sy) && mp[sx][sy] == '.') {
        q.push(NODE(sx, sy));
        book[sx][sy] = true;
    }
    while (!q.empty()) {
        NODE now = q.front();
        q.pop();
        for (int i = 0; i <= 3; i++) {
            int tx = now.x + nextn[i][0];
            int ty = now.y + nextn[i][1];
            if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
            if (check(tx, ty) && !book[tx][ty] && (mp[tx][ty] == '.' || mp[tx][ty] == 'G')) {
                book[tx][ty] = true;
                q.push(NODE(tx, ty));
            }
        }
    }
}
int main() {
    int t; read(t); while (t--) {
        memset(book, false, sizeof(book));
        read(n), read(m);
        for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
        bfs(n, m);
        bool flag = false;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (mp[i][j] == 'G' && !book[i][j]) flag = true;
            }
        }
        if (!flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


你可能感兴趣的:(codeforces)