|
| 1 | +/* |
| 2 | + written by Pankaj Kumar. |
| 3 | + country:-INDIA |
| 4 | +*/ |
| 5 | +#include <bits/stdc++.h> |
| 6 | +using namespace std; |
| 7 | +typedef long long ll; |
| 8 | + |
| 9 | +/* ascii value |
| 10 | +A=65,Z=90,a=97,z=122 |
| 11 | +*/ |
| 12 | + |
| 13 | +// Techniques : |
| 14 | +// divide into cases, brute force, pattern finding |
| 15 | +// sort, greedy, binary search, two pointer |
| 16 | +// transform into graph |
| 17 | +int flag=0; |
| 18 | +int h,w; |
| 19 | +vector<vector<char>> grid; |
| 20 | +vector<vector<bool>> check; |
| 21 | + |
| 22 | +void dfs(int i,int j,int count){ |
| 23 | + if(i<0 || j<0 || i>=h || j>=w){ |
| 24 | + return; |
| 25 | + } |
| 26 | + if(check[i][j]==true){ |
| 27 | + return; |
| 28 | + } |
| 29 | + if(grid[i][j]=='#'){ |
| 30 | + return; |
| 31 | + } |
| 32 | + if(grid[i][j]=='S' && count>0){ |
| 33 | + flag+=(count>=4); |
| 34 | + return; |
| 35 | + } |
| 36 | + check[i][j]=true; |
| 37 | + dfs(i+1,j,count+1); |
| 38 | + dfs(i-1,j,count+1); |
| 39 | + dfs(i,j+1,count+1); |
| 40 | + dfs(i,j-1,count+1); |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +int solve() |
| 45 | +{ |
| 46 | + cin>>h>>w; |
| 47 | + int intialI = 0, intialJ = 0; |
| 48 | + |
| 49 | + grid.resize(h,vector<char>(w)); |
| 50 | + check.resize(h,vector<bool>(w,false)); |
| 51 | + |
| 52 | + for(int i=0;i<h;i++){ |
| 53 | + for(int j=0;j<w;j++){ |
| 54 | + cin>>grid[i][j]; |
| 55 | + if(grid[i][j]=='S'){ |
| 56 | + intialI = i; |
| 57 | + intialJ = j; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + dfs(intialI+1,intialJ,1); |
| 62 | + dfs(intialI-1,intialJ,1); |
| 63 | + dfs(intialI,intialJ+1,1); |
| 64 | + dfs(intialI,intialJ-1,1); |
| 65 | + if(flag){ |
| 66 | + cout<<"Yes"<<endl; |
| 67 | + } |
| 68 | + else{ |
| 69 | + cout<<"No"<<endl; |
| 70 | + } |
| 71 | + return 0; |
| 72 | +} |
| 73 | +int main() |
| 74 | +{ |
| 75 | + long long testCase; |
| 76 | + testCase = 1; |
| 77 | + |
| 78 | + while (testCase--) |
| 79 | + { |
| 80 | + solve(); |
| 81 | + } |
| 82 | + return 0; |
| 83 | +} |
| 84 | +/* -----------------END OF PROGRAM --------------------*/ |
| 85 | +/* |
| 86 | + * stuff you should look before submission |
| 87 | + * constraint and time limit |
| 88 | + * int overflow |
| 89 | + * special test case (n=0||n=1||n=2) |
| 90 | + * don't get stuck on one approach if you get wrong answer |
| 91 | + */ |
0 commit comments