-
Notifications
You must be signed in to change notification settings - Fork 1
/
DSA04001.cpp
61 lines (60 loc) · 1.04 KB
/
DSA04001.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
using namespace std;
int a[100][100];
int n,ok;
string s;
vector<string> vec;
void Try(int i,int j,string s)
{
if(a[1][1]==0)
{
ok=0;
return;
}
if(i==n and j==n and a[n][n]==1)
{
ok=1;
vec.push_back(s);
return;
}
if(i<n and a[i+1][j]==1)
{
Try(i+1,j,s+"D");
}
if(j<n and a[i][j+1]==1)
{
Try(i,j+1,s+"R");
}
if((i<n and j<n and a[i+1][j]==0 and a[i][j+1]==0) or i>n or j>n or i<=0 or j<=0)
{
return;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
vec.clear();
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin>>a[i][j];
}
ok = 0;
s = "";
Try(1,1,s);
if(ok)
{
sort(vec.begin(),vec.end());
for(auto i: vec)
{
cout<<i<<" ";
}
}
else cout<<-1;
cout<<endl;
}
}