-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnight.java
72 lines (61 loc) · 1.63 KB
/
Knight.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
package graph;
/*
* [백준] 나이트의 이동
* https://www.acmicpc.net/problem/7562
*/
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
public class Knight {
static int N, I, Count, curX, curY, goalX, goalY, map[][];
static Queue<KnightPoint> Q;
static final int[] dX = {-2, -2, -1, -1, 1, 1, 2, 2};
static final int[] dY = { 1, -1, 2, -2, 2, -2, 1, -1};
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
N = reader.nextInt();
for(int i=0; i<N; i++) {
I = reader.nextInt();
curX = reader.nextInt();
curY = reader.nextInt();
goalX = reader.nextInt();
goalY = reader.nextInt();
// 계산
bfs();
System.out.println(map[goalY][goalX]);
}
reader.close();
}
private static void bfs() {
map = new int[I][I]; // default 0
// 예외처리
if(curX==goalX && curY==goalY) return;
// 초기화
Q = new LinkedList<KnightPoint>();
Q.add(new KnightPoint(curY, curX));
KnightPoint cur;
while(Q.size() != 0) {
cur = Q.poll();
int x, y;
for(int i=0; i<dX.length; i++) {
x = cur.x + dX[i];
y = cur.y + dY[i];
if (x<0 || x>=I || y<0 || y>=I) continue;
if (map[y][x]==0) {
map[y][x] = map[cur.y][cur.x]+1;
Q.add(new KnightPoint(y, x));
}
if (x==goalX && y==goalY) return; // 목표지점에 도달하면 끝냄
}
}
}
}
// cf. 패키지 안에 Point 클래스가 있어서 KnightPoint라고 이름을 만들었지만
// 단순히 점을 의미하는 것이므로 Point로 하고자했다..
class KnightPoint {
int x, y;
KnightPoint(int y, int x) {
this.x = x;
this.y = y;
}
}