Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- //#define FILE 10
- #define int int64_t
- using namespace std;
- using namespace __gnu_pbds;
- template<typename T>
- using orset = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- template<typename T, typename K>
- using ormap = tree <T, K, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
- const int N = 1e2;
- int ans = 0;
- char a [N + 2] [N + 2];
- bool visited [N + 2] [N + 2];
- int n, m;
- void dfs(pair <int, int> koord)
- {
- if (a [koord.first] [koord.second] == 'G')
- ans++;
- if (a [koord.first] [koord.second + 1] != '#' && !visited [koord.first] [koord.second + 1])
- {
- visited [koord.first] [koord.second + 1] = 1;
- dfs({koord.first, koord.second + 1});
- }
- if (a [koord.first] [koord.second - 1] != '#' && !visited [koord.first] [koord.second - 1])
- {
- visited [koord.first] [koord.second - 1] = 1;
- dfs({koord.first, koord.second - 1});
- }
- if (a [koord.first + 1] [koord.second] != '#' && !visited [koord.first + 1] [koord.second])
- {
- visited [koord.first + 1] [koord.second] = 1;
- dfs({koord.first + 1, koord.second});
- }
- if (a [koord.first - 1] [koord.second] != '#' && !visited [koord.first - 1] [koord.second])
- {
- visited [koord.first - 1] [koord.second] = 1;
- dfs({koord.first - 1, koord.second});
- }
- }
- void solve()
- {
- ans = 0;
- cin >> n >> m;
- for (int i = 0; i < n + 2; i++)
- {
- for (int j = 0; j < m + 2; j++)
- a [i] [j] = '#';
- }
- for (int i = 1; i <= n; i++)
- {
- for (int j = 1; j <= m; j++)
- cin >> a [i] [j];
- }
- if (a [n] [m] == 'B')
- {
- cout << "No\n";
- return;
- }
- for (int i = 1; i <= n; i++)
- {
- for (int j = 1; j <= m; j++)
- {
- if (a [i] [j] == 'B')
- {
- if (a [i + 1] [j] == '.')
- a [i + 1] [j] = '#';
- if (a [i - 1] [j] == '.')
- a [i - 1] [j] = '#';
- if (a [i] [j + 1] == '.')
- a [i] [j + 1] = '#';
- if (a [i] [j - 1] == '.')
- a [i] [j - 1] = '#';
- }
- }
- }
- int num = 0;
- for (int i = 1; i <= n; i++)
- {
- for (int j = 1; j <= m; j++)
- {
- if (a [i] [j] == 'G')
- num++;
- }
- }
- if (a [n] [m] != '#')
- dfs({n, m});
- if (num == ans)
- cout << "Yes\n";
- else
- cout << "No\n";
- }
- int32_t main(int32_t argc, char* argv[]) {
- ios::sync_with_stdio(false);
- cin.tie(0);cout.tie(0);
- #ifdef FILE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int t = 1;
- cin >> t;
- while (t--)
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment