Home [BOJ 7562] 나이트의 이동
Post
Cancel

[BOJ 7562] 나이트의 이동

나이트의 이동 (7562번)

https://www.acmicpc.net/problem/7562

풀이방법

  • 전형적인 BFS 문제이다.
  • X, Y가 움직일 수 있는 모든 경우의 수 8가지를 이용해 문제를 해결한다.
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
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        StringTokenizer st;
        int I = 0;
        StringBuilder sb = new StringBuilder();
        while (T-- > 0) {
            I = Integer.parseInt(br.readLine());

            int[] curPos = new int[2];
            st = new StringTokenizer(br.readLine());
            curPos[0] = Integer.parseInt(st.nextToken());
            curPos[1] = Integer.parseInt(st.nextToken());

            st = new StringTokenizer(br.readLine());
            int[] targetPos = new int[2];
            targetPos[0] = Integer.parseInt(st.nextToken());
            targetPos[1] = Integer.parseInt(st.nextToken());

            int[][] dist = new int[I+1][I+1];
            initDist(dist);

            if (curPos[0] == targetPos[0] && curPos[1] == targetPos[1]) {
                sb.append(0).append('\n');
            } else {
                bfs(dist, curPos, targetPos, I);
                sb.append(dist[targetPos[1]][targetPos[0]]).append('\n');
            }
        }

        System.out.println(sb);
    }

    private static void initDist(int[][] dist) {
        for(int i=0; i<dist.length; i++) {
            for (int j=0; j<dist.length; j++) {
                dist[i][j] = -1;
            }
        }
    }

    private static void bfs(int[][] dist, int[] curPos, int[] targetPos, int I) {
        Queue<int[]> queue = new LinkedList<>();
        queue.add(curPos);
        dist[curPos[1]][curPos[0]] = 0;

        int[] cur;
        int[] xPos = {-1, -1, 1, 1, -2, -2, 2, 2};
        int[] yPos = {2, -2, 2, -2, 1, -1, 1, -1};
        int dx = 0; int dy = 0;
        while (!queue.isEmpty()) {
            cur = queue.poll();

            for(int i=0; i<8; i++) {
                dx = cur[0] + xPos[i];
                dy = cur[1] + yPos[i];

                if (dx < 0 || dx >= I || dy < 0 || dy >= I) continue;
                if (dist[dy][dx] != -1) continue;
                dist[dy][dx] = dist[cur[1]][cur[0]] + 1;

                if (dx == targetPos[0] && dy == targetPos[1]) return;
                queue.add(new int[] { dx, dy });
            }
        }
    }
}

This post is licensed under CC BY 4.0 by the author.

[BOJ 7569] 토마토

[BOJ 11729] 하노이 탑 이동 순서

Comments powered by Disqus.