-
Notifications
You must be signed in to change notification settings - Fork 1
/
Baekjoon1600.java
130 lines (102 loc) · 4.4 KB
/
Baekjoon1600.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
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
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
/**
* https://www.acmicpc.net/problem/1600
* 백준 말이 되고픈 원숭이
*/
public class Baekjoon1600 {
private static int[][] map;
private static int[][] visited;
private static int[][] chance;
private static int[] dx = {1, 0, -1, 0};
private static int[] dy = {0, 1, 1, -1};
private static int[] horseX = {1, 2, -2, -1};
private static int[] horseY = {2, 1, 1, 2};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int K = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int W = Integer.parseInt(st.nextToken());
int H = Integer.parseInt(st.nextToken());
map = new int[W][H];
visited = new int[W][H];
chance = new int[W][H];
for (int i = 0; i < H; i++) {
st = new StringTokenizer(br.readLine());
Arrays.fill(visited[i], Integer.MAX_VALUE);
for (int j = 0; j < W; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
bfs(new Node(0, 0, 0), new Node(W - 1, H - 1, 0), K);
}
private static void bfs(Node start, Node end, int K) {
Queue<Node> queue = new LinkedList();
visited[start.x][start.y] = 0;
chance[start.x][start.y] = 0;
queue.add(start);
while (!queue.isEmpty()) {
Node now = queue.poll();
if (now.equals(end)) {
System.out.println(visited[end.x][end.y]);
return;
}
if (now.usedChance < K) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
int check = j == 0 ? 1 : -1;
int nextX = horseX[i] + now.x;
int nextY = horseY[i] * check + now.y;
if (nextX < 0 || nextY < 0 || nextX >= map[0].length || nextY >= map.length) continue;
if (map[nextX][nextY] == 1) continue;
if (visited[nextX][nextY] > visited[now.x][now.y] + 1) {
visited[nextX][nextY] = visited[now.x][now.y] + 1;
chance[nextX][nextY] = now.usedChance + 1;
queue.add(new Node(nextX, nextY, now.usedChance + 1));
}
if(visited[nextX][nextY] == visited[now.x][now.y]+1 && chance[nextX][nextY] != chance[now.x][now.y]+1){
visited[nextX][nextY] = visited[now.x][now.y] + 1;
chance[nextX][nextY] = now.usedChance + 1;
queue.add(new Node(nextX, nextY, now.usedChance + 1));
}
}
}
}
for (int i = 0; i < 4; i++) {
int nextX = dx[i] + now.x;
int nextY = dy[i] + now.y;
if (nextX < 0 || nextY < 0 || nextX >= map[0].length || nextY >= map.length) continue;
if (map[nextX][nextY] == 1) continue;
if (visited[nextX][nextY] > visited[now.x][now.y] + 1) {
visited[nextX][nextY] = visited[now.x][now.y] + 1;
chance[nextX][nextY] = now.usedChance;
queue.add(new Node(nextX, nextY, now.usedChance));
}
if(visited[nextX][nextY] == visited[now.x][now.y]+1 && chance[nextX][nextY] != chance[now.x][now.y]){
visited[nextX][nextY] = visited[now.x][now.y] + 1;
chance[nextX][nextY] = now.usedChance;
queue.add(new Node(nextX, nextY, now.usedChance));
}
}
}
if (visited[map[0].length - 1][map.length - 1] == Integer.MAX_VALUE) {
System.out.println(-1);
return;
}
System.out.println(visited[map[0].length - 1][map.length - 1]);
}
private static class Node {
int x, y, usedChance;
public Node(int x, int y, int usedChance) {
this.x = x;
this.y = y;
this.usedChance = usedChance;
}
}
}