題目
題意:
你有一個地圖,總共有種符號,’.’,’#’,‘’,‘’,分別代表着路,牆,好人,壞人,現在你可以將路變成牆,問最後是否有一種方案使得壞人走不到好人可以。
思路:
從點開始,當到達某個點的時候,假如這個點旁邊有壞人的話,那麼這個點肯定要做牆,因爲後,這個點是可到達的,所以這個肯定要當作牆,然後最後的時候將地圖遍歷一遍,如果這個點是好人並且是可到達的,那麼就輸出,否則就是。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
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;
}