- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2178.java
More file actions
Latest commit
69 lines (53 loc) · 1.61 KB
/
Copy path2178.java
File metadata and controls
69 lines (53 loc) · 1.61 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
importjava.util.*;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
intn = sc.nextInt();
intm = sc.nextInt();
sc.nextLine();
int[][] map = newint[n][m];
for(inti=0; i<n; i++) {
String[] nums = sc.nextLine().trim().split("");
for(intj=0; j<m; j++) {
map[i][j] = Integer.valueOf(nums[j]);
}
}
int[] dx = {0, 0, 1, -1};
int[] dy = {1, -1, 0, 0};
List<Integer> arr = newLinkedList<>();
Queue<Route> q = newLinkedList<>();
q.offer(newRoute(0,0, 1, newboolean[n][m]));
intx,y;
while(!(q.isEmpty())) {
Router = q.poll();
if(r.x == n-1 && r.y == m-1) {
arr.add(r.cost);
continue;
}
for(inti=0; i<4; i++) {
x = r.x + dx[i];
y = r.y + dy[i];
if(x>=0 && x<n && y>=0 && y<m && map[x][y] == 1 && !(r.visited[x][y])) {
boolean[][] visited = Arrays.copyOf(r.visited, n);
visited[x][y] = true;
q.offer(newRoute(x, y, r.cost+1, visited));
}
}
}
Collections.sort(arr);
System.out.println(arr.get(0));
}
}
classRoute {
intx;
inty;
intcost;
boolean[][] visited;
publicRoute(intx, inty, intcost, boolean[][] visited) {
this.x = x;
this.y = y;
this.cost = cost;
this.visited = visited;
visited[x][y] = true;
}
}