[BOJ] 4485 녹색 옷 입은 애가 젤다지?

Time Lapse :NONE

4485.cpp

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
#include <stdio.h>
#include <memory.h>
#include <queue>
#include <vector>
using namespace std;
int N, cave[125][125];
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
class link {
public:
int x;
int y;
int cost;
link(int _x, int _y, int _c) : x(_x), y(_y), cost(_c){}
};
bool operator< (link a, link b) {
return a.cost > b.cost;
}
int solution() {
priority_queue<link> pq;
bool v[125][125] = { false, };
v[0][0] = true;
pq.emplace(0, 0, cave[0][0]);
while (!pq.empty()) {
int x = pq.top().x;
int y = pq.top().y;
int cost = pq.top().cost;
pq.pop();
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx == N - 1 && ny == N - 1) return cost + cave[ny][nx];
if (0 <= nx && nx < N && 0 <= ny && ny < N && !v[ny][nx]) {
v[ny][nx] = true;
pq.emplace(nx, ny, cost + cave[ny][nx]);
}
}
}
return -1;
}
int main(void) {
int p = 0;
while (1) {
scanf("%d", &N);
if (!N) break;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
scanf("%d", &cave[i][j]);
printf("Problem %d: %d\n", ++p,solution());
}

return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/4485/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.