-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathShortestSourceToDestinationPath.java
49 lines (40 loc) · 1.17 KB
/
ShortestSourceToDestinationPath.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
/*https://practice.geeksforgeeks.org/problems/shortest-source-to-destination-path3544/1/*/
class Solution {
class Node
{
int x,y,count;
Node(int x,int y,int count)
{
this.x=x;
this.y=y;
this.count=count;
}
}
int shortestDistance(int N, int M, int A[][], int X, int Y)
{
if(A[0][0]==0)
return -1;
Queue<Node> q=new LinkedList<>();
q.offer(new Node(0,0,0));
int dirx[]={1,-1,0,0};
int diry[]={0,0,-1,1};
boolean visited[][]=new boolean[N][M];
while(q.size()>0)
{
Node nd=q.remove();
if(nd.x==X&&nd.y==Y)
return nd.count;
for(int i=0;i<dirx.length;i++)
{
int x=nd.x+dirx[i];
int y=nd.y+diry[i];
if(x>=A.length||x<0||y>=A[0].length||y<0||visited[x][y])
continue;
visited[x][y]=true;
if(A[x][y]==1)
q.offer(new Node(x,y,nd.count+1));
}
}
return -1;
}
}