forked from BitsPleaseMSI/BitsPlease-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.cpp
54 lines (48 loc) · 1006 Bytes
/
5.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
/* Amit Bansal - @amitbansal7 */
#include <bits/stdc++.h>
using namespace std;
bool ans;
void solve(vector<vector<int> >&A, int i, int j)
{
if (i < 0 || j < 0 || i >= A.size() || j >= A[0].size() || A[i][j] == 0 || ans)
return;
if (A[i][j] == 2)
{
ans = 1;
return;
}
A[i][j] = 0;
solve(A, i + 1, j);
solve(A, i - 1, j);
solve(A, i, j - 1);
solve(A, i, j + 1);
A[i][j] = 1;
}
int main(int argc, char const *argv[])
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<vector<int> >A(n, vector<int>(n,0));
int a,b;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> A[i][j];
if(A[i][j] == 1)
{
a = i;
b = j;
}
}
}
ans = 0;
solve(A, a, b);
cout << ans << endl;
}
return 0;
}