| 4 | vector<vector<int>> nb = {{0,1},{0,-1},{1,0},{-1,0}}; |
| 5 | |
| 6 | bool helper(vector<vector<int>>& mat,int x,int y,int key,vector<vector<int>>& vis){ |
| 7 | vis[x][y]=1; |
| 8 | bool ans=0; |
| 9 | for(int i =0; i < 4; i++){ |
| 10 | int x1 = x+nb[i][0]; |
| 11 | int y1 = x+nb[i][1]; |
| 12 | if(x1 > -1 && x1 < mat.size() && y1 > -1 && y1 < mat[0].size() && mat[x1][y1]==key){ |
| 13 | cout<<" inner loop "<<x1<<" "<<y1<<" "<<ans<<endl; |
| 14 | if( !vis[x1][y1]){ |
| 15 | ans |=helper(mat,x1,y1,key,vis); |
| 16 | } |
| 17 | else if( vis[x1][y1] && (x1 != x || y1 != y)){ |
| 18 | return true; |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | cout<<x<<" "<<y<<" "<<ans<<endl; |
| 23 | return ans; |
| 24 | } |
| 25 | |
| 26 | bool solve(vector<vector<int>>& mat) { |
| 27 | vector<vector<int>> vis(mat.size(),vector<int>(mat[0].size(),0)); |