Skip to content

Commit e7665ed

Browse files
authored
[BOJ] 1600 말이 되고픈 원숭이 (G3)
1 parent aeb972d commit e7665ed

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

박예진/0주차/BFS/1600.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//https://www.acmicpc.net/problem/1600
2+
3+
#include <iostream>
4+
#include <vector>
5+
#include <algorithm>
6+
#include <queue>
7+
using namespace std;
8+
9+
/*
10+
1 장애물
11+
0, 0 -> H - 1, W - 1
12+
최소한의 동작
13+
K번만 말처럼 움직일 수 있음
14+
*/
15+
16+
struct Node {
17+
int x, y, hcnt, res;
18+
};
19+
20+
int K, W, H;
21+
int arr[201][201];
22+
bool visited[201][201][31];
23+
const int dx[4] = {-1, 0, 1, 0};
24+
const int dy[4] = {0, 1, 0, -1};
25+
const int hdx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
26+
const int hdy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
27+
28+
bool OOB(int x, int y) {
29+
return x < 0 || x >= H || y < 0 || y >= W;
30+
}
31+
32+
int bfs(int x, int y) {
33+
queue<Node> q;
34+
q.push({x, y});
35+
visited[x][y][0] = true;
36+
37+
while(!q.empty()) {
38+
Node now = q.front();
39+
q.pop();
40+
41+
if (now.x == H - 1 && now.y == W - 1) {
42+
return now.res;
43+
}
44+
45+
// 원숭이 이동
46+
for(int dir = 0; dir < 4; dir++){
47+
int nx = now.x + dx[dir];
48+
int ny = now.y + dy[dir];
49+
if (OOB(nx, ny) || visited[nx][ny][now.hcnt] || arr[nx][ny] == 1) continue;
50+
visited[nx][ny][now.hcnt] = true;
51+
q.push({nx, ny, now.hcnt, now.res + 1});
52+
}
53+
54+
// 말처럼 이동
55+
if (now.hcnt < K) {
56+
for(int dir = 0; dir < 8; dir++){
57+
int nx = now.x + hdx[dir];
58+
int ny = now.y + hdy[dir];
59+
if (OOB(nx, ny) || visited[nx][ny][now.hcnt + 1] || arr[nx][ny] == 1) continue;
60+
visited[nx][ny][now.hcnt + 1] = true;
61+
q.push({nx, ny, now.hcnt + 1, now.res + 1});
62+
}
63+
}
64+
}
65+
return -1;
66+
}
67+
68+
int main(){
69+
ios_base::sync_with_stdio(false);
70+
cin.tie(NULL); cout.tie(NULL);
71+
72+
cin >> K >> W >> H;
73+
for(int i = 0; i < H; i++){
74+
for(int j = 0; j < W; j++){
75+
cin >> arr[i][j];
76+
}
77+
}
78+
cout << bfs(0, 0);
79+
80+
return 0;
81+
}

0 commit comments

Comments
 (0)