Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
- #define all(v) v.begin(),v.end()
- #define rall(v) v.rbegin(),v.rend()
- #define MOD 1000000007
- #define PI 3.14159265
- #define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
- #define imin INT_MIN
- #define imax INT_MAX
- #define nl '\n'
- #define modulo(a, b, m) ((a % m) * (b % m)) % m
- void Start_Crushing() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
- int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
- vector<vector<char>> grid;
- vector<vector<bool>> vis;
- int n, m;
- bool isValid(int i, int j){
- return i <= n and j <= m and i > 0 and j > 0 and grid[i][j] != '*' and !vis[i][j];
- }
- bool pathFound(int x, int y){
- // if(x == n - 1 and y == m - 1) return grid[x][y] == 'E';
- if(grid[x][y] == 'E') return true;
- vis[x][y] = true;
- bool thereIs = false;
- for(int i = 0; i < 4; i++){
- int newX = x + dx[i], newY = y + dy[i];
- if(isValid(newX, newY)){
- thereIs |= pathFound(newX, newY);
- }
- }
- return thereIs;
- }
- void solve(){
- cin >> n >> m;
- grid.assign(n + 5, vector<char>(m + 5, '.'));
- vis.assign(n + 5, vector<bool>(m + 5, false));
- pair<int, int> start;
- for(int i = 1; i <= n; i++){
- for(int j = 1; j <= m; j++){
- cin >> grid[i][j];
- if(grid[i][j] == 'S'){
- start = {i, j};
- }
- }
- }
- bool ans = pathFound(start.first, start.second);
- cout << (ans ? "YES" : "NO");
- }
- int main(){
- // freopen("equal.in", "r", stdin);
- Start_Crushing();
- int t = 1;
- // /*is Single Test case?*/ cin >> t;
- while (t--) {
- solve();
- if(!t) break;
- cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement