Skip to content

Commit 073730b

Browse files
committed
[Gold III] Title: 말이 되고픈 원숭이, Time: 72 ms, Memory: 7936 KB -BaekjoonHub
1 parent 6311ca5 commit 073730b

2 files changed

Lines changed: 73 additions & 61 deletions

File tree

백준/Gold/1600. 말이 되고픈 원숭이/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 7936 KB, 시간: 76 ms
7+
메모리: 7936 KB, 시간: 72 ms
88

99
### 분류
1010

1111
그래프 이론, 그래프 탐색, 너비 우선 탐색
1212

1313
### 제출 일자
1414

15-
2025년 11월 7일 21:55:04
15+
2025년 11월 7일 22:06:38
1616

1717
### 문제 설명
1818

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,83 @@
1-
#include<iostream>
2-
#include<queue>
3-
#include<tuple>
1+
#include <iostream>
2+
#include <queue>
3+
#include <tuple>
4+
#include <cstring>
5+
46
using namespace std;
57

8+
#define MAX 205
9+
610
int 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

1319
void 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

6671
int 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

Comments
 (0)