Advertisement
Guest User

Untitled

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