-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain2.java
87 lines (69 loc) Β· 2.58 KB
/
Main2.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
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
package BFS.P1600;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main2 {
static int K, W, H;
static int[][] map;
static int[][] delta = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
static int[][] horse = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BFS/P1600/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
K = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
W = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
map = new int[H][W];
for (int i = 0; i < H; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < W; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
System.out.println(bfs());
}
static int bfs() {
Queue<Pos> q = new LinkedList<>();
boolean[][][] visited = new boolean[H][W][K+1];
q.offer(new Pos(0, 0, K, 0));
visited[0][0][K] = true;
while (!q.isEmpty()) {
Pos p = q.poll();
if (p.i == H-1 && p.j == W-1) return p.dist;
for (int k = 0; k < 4; k++) {
int to_i = p.i + delta[k][0];
int to_j = p.j + delta[k][1];
if (isValidPath(to_i, to_j) && map[to_i][to_j] == 0 && !visited[to_i][to_j][p.k]) {
visited[to_i][to_j][p.k] = true;
q.offer(new Pos(to_i, to_j, p.k, p.dist+1));
}
}
if (p.k == 0) continue;
for (int k = 0; k < 8; k++) {
int to_i = p.i + horse[k][0];
int to_j = p.j + horse[k][1];
if (isValidPath(to_i, to_j) && map[to_i][to_j] == 0 && !visited[to_i][to_j][p.k-1]) {
visited[to_i][to_j][p.k-1] = true;
q.offer(new Pos(to_i, to_j, p.k-1, p.dist+1));
}
}
}
return -1;
}
static boolean isValidPath(int i, int j) {
return 0 <= i && i < H && 0 <= j && j < W;
}
static class Pos {
int i, j, k, dist;
public Pos(int i, int j, int k, int dist) {
this.i = i;
this.j = j;
this.k = k;
this.dist = dist;
}
}
}