-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathMatPathDFS.java
More file actions
170 lines (118 loc) · 4.95 KB
/
MatPathDFS.java
File metadata and controls
170 lines (118 loc) · 4.95 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package Backtracking;
/**
Given a MxN matrix where each element can either be 0 or 1.
We need to find the shortest path between a given source cell to a destination cell.
The path can only be created out of a cell if its value is 1.
Expected time complexity is O(MN).
For example –
Input:
mat[ROW][COL] = {{1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
{1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
{0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
{1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }};
Source = {0, 0};
Destination = {3, 4};
Output:
Shortest Path is 11
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Given a boolean 2D matrix (0-based index), find whether there is path from (0,0) to (x,y) and if there is one path, print the minimum no of steps needed to reach it, else print -1 if the destination is not reachable. You may move in only four direction ie up, down, left and right. The path can only be created out of a cell if its value is 1.
*
* Input:
* The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines . The first line of each test case contains two integers n and m denoting the size of the matrix. Then in the next line are n*m space separated values of the matrix. The following line after it contains two integers x and y denoting the index of the destination.
*
* Output:
* For each test case print in a new line the min no of steps needed to reach the destination.
*
* Constraints:
* 1<=T<=100
* 1<=n,m<=20
*
* Example:
* Input:
* 2
* 3 4
* 1 0 0 0 1 1 0 1 0 1 1 1
* 2 3
* 3 4
* 1 1 1 1 0 0 0 1 0 0 0 1
* 0 3
* Output:
* 5
* 3
*/
public class MatPathDFS {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-->0){
String line = br.readLine();
String[] strs = line.trim().split("\\s+");
int m = Integer.parseInt(strs[0]);
int n = Integer.parseInt(strs[1]);
line = br.readLine();
strs = line.trim().split("\\s+");
int k=0;
int mat[][] = new int[m][n];
for (int i=0; i<m; i++){
for (int j=0; j<n; j++){
mat[i][j] = Integer.parseInt(strs[k++]);
}
}
int[] src = new int[]{0,0};
strs = br.readLine().split("\\s+");
int[] dest = new int[]{Integer.parseInt(strs[0]), Integer.parseInt(strs[1])};
int shortest = getShorted(mat, n, m, src, dest);
System.out.println(shortest);
}
}
private static int getShorted(int[][] mat, int n, int m, int[] src, int[] dest) {
boolean[][] visited = new boolean[m][n];
for (int i=0; i<m; i++){
for (int j=0; j<n; j++){
visited[i][j] = mat[i][j] != 1;
}
}
int shortest = visitedUtil(mat, visited, n, m, src, dest, 0, Integer.MAX_VALUE);
if (shortest == Integer.MAX_VALUE)
return -1;
else
return shortest;
}
private static int visitedUtil(int[][] mat, boolean[][] visited, int n, int m, int[] src, int[] dest, int dist, int shortest) {
int sx = src[0];
int sy = src[1];
int dx = dest[0];
int dy = dest[1];
if (!isValid(sx, sy, m, n, mat, visited))
return Integer.MAX_VALUE;
if (dx == sx && dy == sy)
return Math.min(shortest,dist);
visited[sx][sy] = true;
//left
if (isValid(sx,sy-1, m, n, mat, visited))
shortest = visitedUtil(mat, visited, n, m, new int[]{sx, sy-1}, dest, dist+1, shortest);
//up
if (isValid(sx-1,sy, m, n, mat, visited))
shortest = visitedUtil(mat, visited, n, m, new int[]{sx-1, sy}, dest, dist+1, shortest);
//down
if (isValid(sx+1,sy, m, n, mat, visited))
shortest = visitedUtil(mat, visited, n, m, new int[]{sx+1, sy}, dest, dist+1, shortest);
//right
if (isValid(sx,sy+1, m, n, mat, visited))
shortest = visitedUtil(mat, visited, n, m, new int[]{sx, sy+1}, dest, dist+1, shortest);
visited[sx][sy] = false;
return shortest;
}
private static boolean isValid(int sx, int sy, int m, int n, int[][] mat, boolean[][] visited) {
return sx >= 0 && sx < m && sy >= 0 && sy < n && mat[sx][sy] == 1 && !visited[sx][sy];
}
}