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


방문 처리 배열을 3차원으로 만들어 준다. 나이트처럼 이동하는 것을 점프라고 하면, 


[row][col][이 지점에서 가능한 점프 횟수]


위와 같이 설정해준다.



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
#include<iostream>
#include<queue>
 
using namespace std;
int map[201][201], row, col, k;
bool  vis[201][201][31]; //r, c, 점프 가능 횟수
struct Info {
    int r, c, jump, cnt;
};
 
queue<Info> q;
const int dr[4= { 0,0,1,-1 };
const int dc[4= { 1,-1,0,0 };
const int jr[8= { -2,-1,1,2,2,1,-1,-2 };
const int jc[8= { 1,2,2,1,-1,-2,-2,-1 };

void bfs() {
    q.push({ 00, k, 0 });
    vis[0][0][k] = true;
    while (!q.empty()) {
        int qs = q.size();
        while (qs--) {
            
            Info cur = q.front();
            q.pop();
            if (cur.r == row - 1 && cur.c == col - 1) {
                cout << cur.cnt;
                return;
            }
            for (int i = 0; i < 4; i++) {
                int nr = cur.r + dr[i];
                int nc = cur.c + dc[i];
                if (nr < 0 || nc < 0 || nr >= row || nc >= col || map[nr][nc] == 1continue;
                
                if (vis[nr][nc][cur.jump]) continue//z만큼 점프 개수를 남기고 온적이 있으면 pass
                q.push({ nr, nc, cur.jump, cur.cnt + 1 });
                vis[nr][nc][cur.jump] = true;
                
            }
 
            if (cur.jump <= 0continue;
            for (int i = 0; i < 8 ; i++) {
                int nr = cur.r + jr[i];
                int nc = cur.c + jc[i];
                if (nr < 0 || nc < 0 || nr >= row || nc >= col || map[nr][nc] == 1continue;
                
                if (vis[nr][nc][cur.jump - 1]) continue//z만큼 점프 개수를 남기고 온적이 있으면 pass
                q.push({ nr, nc, cur.jump - 1, cur.cnt + 1 });
                vis[nr][nc][cur.jump - 1= true;
            }
 
        }
    }
    cout << -1;
}
 
int main(void) {
    cin >> k >> col >> row;
    for (int i = 0; i < row; i++
        for (int j = 0; j < col; j++
            cin >> map[i][j];
            
    bfs();
 
    return 0;
}
cs


+ Recent posts