Post

[BaekJoon] 1600번 - 말이 되고픈 원숭이 [Java][C++]

[BaekJoon] 1600번 - 말이 되고픈 원숭이 [Java][C++]

문제 링크


1. 아이디어


원숭이는 기본적으로 상하좌우로만 움직일 수 있지만 최대 K번은 말처럼 움직일 수 있다. 최단 거리 BFS를 통해 해결할 수 있는데 이때 특정 좌표를 단순히 방문하는 것만 체크하는게 아니라 말처럼 몇 번 움직여서 방문했는지를 체크하면 두 움직임을 조합한 최단 거리를 구할 수 있다. 이를 위해 3차원 배열로 방문 체크를 만들어서 말처럼 움직일 수 있는 횟수가 남아 있으면 이 이동 방식도 큐에 삽입하는 방식으로 해결했다.


2. 코드


1. 풀이 [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
import java.io.*;
import java.util.*;

public class Main {

    static int[] dr = {-1, 0, 1, 0};
    static int[] dc = {0, 1, 0, -1};
    static int[] drh = {-1, -2, -2, -1, 1, 2, 2, 1};
    static int[] dch = {-2, -1, 1, 2, 2, 1, -1, -2};
    static int h, w;
    static int[][] grid;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int k = Integer.parseInt(br.readLine());

        st = new StringTokenizer(br.readLine());
        w = Integer.parseInt(st.nextToken());
        h = Integer.parseInt(st.nextToken());

        grid = new int[h][w];
        for (int i = 0; i < h; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < w; j++) {
                grid[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        System.out.println(bfs(k));
    }

    static int bfs(int k) {
        Queue<int[]> q = new ArrayDeque<>();
        q.offer(new int[]{0, 0, 0});

        boolean[][][] vis = new boolean[h][w][1 + k];
        vis[0][0][0] = true;

        int dist = 0;

        while (!q.isEmpty()) {
            int sz = q.size();

            while (sz-- > 0) {
                int[] cur = q.poll();
                if (cur[0] == h - 1 && cur[1] == w - 1) return dist;

                for (int d = 0; d < 4; d++) {
                    int nr = cur[0] + dr[d];
                    int nc = cur[1] + dc[d];

                    if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
                    if (grid[nr][nc] == 1 || vis[nr][nc][cur[2]]) continue;

                    q.offer(new int[]{nr, nc, cur[2]});
                    vis[nr][nc][cur[2]] = true;
                }

                if (cur[2] == k) continue;

                for (int d = 0; d < 8; d++) {
                    int nr = cur[0] + drh[d];
                    int nc = cur[1] + dch[d];

                    if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
                    if (grid[nr][nc] == 1 || vis[nr][nc][cur[2] + 1]) continue;

                    q.offer(new int[]{nr, nc, cur[2] + 1});
                    vis[nr][nc][cur[2] + 1] = true;
                }
            }

            dist++;
        }

        return -1;
    }
}


2. 풀이 [C++]

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
#include <bits/stdc++.h>
using namespace std;

int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int drh[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
int dch[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int k, h, w;
int grid[200][200];
bool vis[200][200][31];

int bfs() {
    queue<tuple<int, int, int>> q;
    q.push({0, 0, 0});

    vis[0][0][0] = true;

    int dist = 0;

    while (!q.empty()) {
        int sz = q.size();

        while (sz--) {
            auto [r, c, t] = q.front();
            q.pop();

            if (r == h - 1 && c == w - 1) return dist;

            for (int d = 0; d < 4; d++) {
                int nr = r + dr[d];
                int nc = c + dc[d];

                if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
                if (grid[nr][nc] == 1 || vis[nr][nc][t]) continue;

                q.push({nr, nc, t});
                vis[nr][nc][t] = true;
            }

            if (t == k) continue;

            for (int d = 0; d < 8; d++) {
                int nr = r + drh[d];
                int nc = c + dch[d];

                if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
                if (grid[nr][nc] == 1 || vis[nr][nc][t + 1]) continue;

                q.push({nr, nc, t + 1});
                vis[nr][nc][t + 1] = true;
            }
        }

        dist++;
    }

    return -1;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> k >> w >> h;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> grid[i][j];
        }
    }

    cout << bfs();
}

3. 디버깅


없음.


4. 참고


없음.


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