-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find_Whether_Path_Exists.java
54 lines (54 loc) · 1.07 KB
/
Find_Whether_Path_Exists.java
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
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static boolean find(int a[][],int r,int c,int n,int m)
{a[r][c]=0;
boolean flag1=false,flag2=false,flag3=false,flag4=false;
if(r-1>=0)
{if(a[r-1][c]==2)
flag1=true;
else if(a[r-1][c]==3)
flag1=find(a,r-1,c,n,m);
}
if(r+1<m)
{if(a[r+1][c]==2)
flag2=true;
else if(a[r+1][c]==3)
flag2=find(a,r+1,c,n,m);
}
if(c-1>=0)
{if(a[r][c-1]==2)
flag3=true;
else if(a[r][c-1]==3)
flag3=find(a,r,c-1,n,m);
}
if(c+1<m)
{if(a[r][c+1]==2)
flag4=true;
else if(a[r][c+1]==2)
flag4=find(a,r,c+1,n,m);
}
return flag1||flag2||flag3||flag4;
}
public static void main (String[] args)
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{int n=s.nextInt();
int a[][]=new int[n][n];
int p=0,q=0;
for(int i=0;i<n;i++)
{for(int j=0;j<n;j++)
{a[i][j]=s.nextInt();
if(a[i][j]==1)
{p=i;q=j;}
}}
if(find(a,p,q,n,n))
System.out.println("1");
else
System.out.println("0");}
}
}