• 문제 링크
15242번: Knight
In Chess, the knight is the weirdest of all the pieces. To begin with, the piece is actually a horse without any human riding it. The second reason is its movement pattern. It can move 2 cells forward and one to the side. Below you can see all the possible
www.acmicpc.net
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String s;
Pair src = new Pair((s = br.readLine()).charAt(0) - 'a', s.charAt(1) - '1');
Pair tgt = new Pair((s = br.readLine()).charAt(0) - 'a', s.charAt(1) - '1');
Queue<Pair> q = new ArrayDeque<>();
int[][] mat = new int[8][8], dir = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}};
q.offer(src);
while (!q.isEmpty()) {
Pair cur = q.poll();
if (cur.y == tgt.y && cur.x == tgt.x) break;
for (int[] d : dir) {
int ny = cur.y + d[0], nx = cur.x + d[1];
if (ny < 0 || ny >= 8 || nx < 0 || nx >= 8 || mat[ny][nx] != 0) continue;
q.offer(new Pair(ny, nx));
mat[ny][nx] = mat[cur.y][cur.x] + 1;
}
}
bw.write(String.valueOf(mat[tgt.y][tgt.x]));
bw.flush();
}
private static class Pair {
int y, x;
Pair(int y, int x) {
this.y = y;
this.x = x;
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 28323 불안정한 수열 - Greedy / Java (0) | 2023.12.18 |
---|---|
[백준] 4993 Red and Black - Graph Theory / Java (0) | 2023.12.17 |
[백준] 6207 Cow Picnic - Graph Theory / Java (0) | 2023.12.15 |
[백준] 18788 Swapity Swap - Graph Theory / Java (0) | 2023.12.14 |
[백준] 6189 Munching - Graph Theory / Java (0) | 2023.12.13 |
댓글