1- #include < iostream>
2- #include < queue>
3- #include < tuple>
1+ #include < iostream>
2+ #include < queue>
3+ #include < tuple>
4+ #include < cstring>
5+
46using namespace std ;
57
8+ #define MAX 205
9+
610int K, W, H;
7- int arr[205 ][205 ]{};
8- int visit[205 ][205 ][35 ]{};
9- int dx[12 ] = { 0 ,1 ,0 ,-1 ,-2 ,-2 ,-1 ,-1 ,1 ,1 ,2 ,2 };
10- int dy[12 ] = { 1 ,0 ,-1 ,0 ,-1 ,1 ,-2 ,2 ,-2 ,2 ,-1 ,1 };
11- queue<tuple<int , int , int , int >>q; // x, y, 이동횟수, 말 점프 카운트
11+ int maps[MAX][MAX]{};
12+ int visited[MAX][MAX][35 ]{};
13+
14+ int dx[12 ] = { 0 ,0 ,1 ,-1 ,-2 ,-2 ,-1 ,-1 ,1 ,1 ,2 ,2 };
15+ int dy[12 ] = { 1 ,-1 ,0 ,0 ,-1 ,1 ,-2 ,2 ,-2 ,2 ,-1 ,1 };
16+ int answer = 1e9 ;
17+ bool flag = false ;
1218
1319void input () {
14- cin >> K >> W >> H;
15- for (int i = 0 ; i < H; i++)
16- for (int j = 0 ; j < W; j++)
17- cin >> arr[i][j];
20+ cin >> K >> W >> H;
21+ for (int i = 0 ; i < H; i++) {
22+ for (int j = 0 ; j < W; j++) {
23+ cin >> maps[i][j];
24+ }
25+ }
1826}
1927
20- void solve () {
21- bool flag = false ;
22-
23- q.push ({ 0 ,0 ,0 ,0 });
24-
25- int ans = 1e9 ;
26-
27- while (!q.empty ()) {
28- int x = get<0 >(q.front ());
29- int y = get<1 >(q.front ());
30- int time = get<2 >(q.front ());
31- int cnt = get<3 >(q.front ());
32- q.pop ();
33-
34- if (x == H - 1 && y == W - 1 ) {
35- ans = ans < time ? ans : time;
36- flag = true ;
37- }
38-
39- for (int i = 0 ; i < 4 ; i++) { // 인접 이동
40- int nx = x + dx[i];
41- int ny = y + dy[i];
42- if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
43- if (!arr[nx][ny] && !visit[nx][ny][cnt]) {
44- visit[nx][ny][cnt]++;
45- q.push ({ nx,ny,time + 1 ,cnt });
46- }
47- }
48- }
49- if (cnt < K) // 말 점프 사용 가능할 때
50- for (int i = 4 ; i < 12 ; i++) {
51- int nx = x + dx[i];
52- int ny = y + dy[i];
53- if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
54- if (!arr[nx][ny] && !visit[nx][ny][cnt + 1 ]) {
55- visit[nx][ny][cnt + 1 ]++;
56- q.push ({ nx,ny,time + 1 ,cnt + 1 }); // 말 점프 카운트 + 1
57- }
58- }
59- }
60- }
61- if (!flag)
62- cout << -1 ;
63- else cout << ans;
28+ void bfs () {
29+ queue<tuple<int , int , int , int >> q;
30+ q.push ({0 , 0 , 0 , 0 }); // x, y, 이동 횟수, 말 점프 카운트
31+
32+ while (!q.empty ()) {
33+ int cx = get<0 >(q.front ());
34+ int cy = get<1 >(q.front ());
35+ int dist = get<2 >(q.front ());
36+ int k_cnt = get<3 >(q.front ());
37+ q.pop ();
38+
39+ // 최종 지점에 도착해야 answer = dist로 업데이트
40+ // 즉, 최종 지점에 도착하지 못하면, answer는 그대로 -1임
41+ if (cx == H - 1 && cy == W - 1 ) {
42+ answer = answer < dist ? answer : dist;
43+ flag = true ;
44+ }
45+
46+ for (int i = 0 ; i < 4 ; i++) {
47+ int nx = cx + dx[i];
48+ int ny = cy + dy[i];
49+
50+ if (nx < 0 || nx >= H || ny < 0 || ny >= W || visited[nx][ny][k_cnt]) continue ;
51+ if (maps[nx][ny]) continue ;
52+
53+ visited[nx][ny][k_cnt]++;
54+ q.push ({nx, ny, dist + 1 , k_cnt});
55+ }
56+ if (k_cnt < K) { // 말 점프 사용 가능할 때
57+ for (int i = 4 ; i < 12 ; i++) {
58+ int nx = cx + dx[i];
59+ int ny = cy + dy[i];
60+
61+ if (nx < 0 || nx >= H || ny < 0 || ny >= W || visited[nx][ny][k_cnt + 1 ]) continue ;
62+ if (maps[nx][ny]) continue ;
63+
64+ visited[nx][ny][k_cnt + 1 ]++;
65+ q.push ({nx, ny, dist + 1 , k_cnt + 1 }); // 말 점프 카운트 + 1
66+ }
67+ }
68+ }
6469}
6570
6671int main () {
67- input ();
68- solve ();
72+ input ();
73+
74+ bfs ();
75+
76+ if (!flag) {
77+ cout << -1 ;
78+ } else {
79+ cout << answer;
80+ }
6981
70- return 0 ;
82+ return 0 ;
7183}
0 commit comments