Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<string> res;
  6. bool visited[20][20] = {false};
  7.  
  8. int a[20][20];
  9. void Try(int row, int col, int n, string s){
  10.     if (row == n && col == n){
  11.         res.push_back(s);
  12.     }
  13.     else{
  14.         if (a[row+1][col] == 1 && visited[row+1][col] == false){
  15.             visited[row+1][col] = true;
  16.             Try(row+1,col,n,s+"D");
  17.             visited[row+1][col] = false;
  18.         }
  19.         if (a[row][col+1] == 1 && visited[row][col+1] == false){
  20.             visited[row][col+1] = true;
  21.             Try(row,col+1,n,s+"R");
  22.             visited[row][col+1] = false;
  23.         }    
  24.         if (a[row-1][col] == 1 && visited[row-1][col] == false){
  25.             visited[row-1][col] = true;
  26.             Try(row-1,col,n,s+"U");
  27.             visited[row-1][col] = false;
  28.        
  29.         }    
  30.         if (a[row][col-1] == 1 && visited[row][col-1] == false){
  31.             visited[row][col-1] = true;
  32.             Try(row,col-1,n,s+"L");
  33.             visited[row][col-1] = false;
  34.         }    
  35.     }
  36. }
  37.  
  38. void process(){
  39.     res.clear();
  40.     for(int i=0;i<=20;i++){
  41.         for(int j=0;j<=20;j++){
  42.             a[i][j] = 0;
  43.             visited[i][j] = false;
  44.         }
  45.     }
  46.     visited[1][1] = true;
  47.     int n;
  48.     cin >> n;
  49.     for(int i=1;i<=n;i++){
  50.         for(int j=1;j<=n;j++){
  51.             cin >> a[i][j];
  52.         }
  53.     }
  54.     Try(1,1,n,"");
  55.     sort(res.begin(),res.end());
  56.     if (!res.size() || a[1][1] == 0){
  57.         cout << -1 << endl;
  58.         return;
  59.     }
  60.     for(int i=0;i<res.size();i++) cout << res[i] << " ";
  61.     cout << endl;
  62.  
  63. }
  64.  
  65. int main(){
  66.    
  67.     int T;
  68.     cin >> T;
  69.     while(T--) process();
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement