Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Archives
Today
Total
관리 메뉴

codingfarm

백준 - 4485. 녹색 옷 입은 애가 젤다지? 본문

Algorithm & Data structure/Problem Solution

백준 - 4485. 녹색 옷 입은 애가 젤다지?

scarecrow1992 2020. 10. 10. 16:06

www.acmicpc.net/problem/4485

 

4485번: 녹색 옷 입은 애가 젤다지?

젤다의 전설 게임에서 화폐의 단위는 루피(rupee)다. 그런데 간혹 '도둑루피'라 불리는 검정색 루피도 존재하는데, 이걸 획득하면 오히려 소지한 루피가 감소하게 된다! 젤다의 전설 시리즈의 주��

www.acmicpc.net

 

상하좌우 이동을 통해 각 칸으로 이동하면 해당 칸에 배정된 점수를 얻게된다.

이때 오른쪽 아래까지 이동하는동안 얻을 수 있는 최소 점수를 찾는 문제이다.

 

풀이

 노드와 간선이 안주어젔을뿐 전형적인 다익스트라 문제이다.

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>
 
#define INF 2100000000
 
using namespace std;
 
int board[150][150];
int dist[150][150];
int N;
 
int moveX[4= { 1,0,-1,0 };
int moveY[4= { 0,1,0,-1 };
 
class Node {
public:
    int x, y, score;
    Node(int x_, int y_, int score_) : x(x_), y(y_), score(score_) {}
    Node() : Node(-1,-1,-1) {}
    bool operator<(const Node& rhs) const {
        return score > rhs.score;
    }
};
 
int Dijkstra() {
    priority_queue<Node> pq;
    pq.push(Node(00, board[0][0]));
    dist[0][0= 0;
    int ret = INF;
    while (!pq.empty()) {
        Node cNode = pq.top();
        pq.pop();
 
        int nx, ny;
        for (int dir = 0; dir < 4; dir++) {
            nx = cNode.x + moveX[dir];
            ny = cNode.y + moveY[dir];
            
            if (nx < 0 || nx >= N || ny < 0 || ny >= N)
                continue;
 
            if (dist[ny][nx] > cNode.score + board[ny][nx]) {
                dist[ny][nx] = cNode.score + board[ny][nx];
                pq.push(Node(nx, ny, dist[ny][nx]));
            }
        }
    }
    return dist[N - 1][N - 1];
}
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int testcase = 1;
    while (1) {
        cin >> N;
        if (N == 0)
            break;
        for (int row = 0; row < N; row++) {
            for (int col = 0; col < N; col++) {
                cin >> board[row][col];
                dist[row][col] = INF;
            }
        }
 
        int val = Dijkstra();
        cout << "Problem " << testcase << ": " << val << "\n";
 
        testcase++;
    }
 
    return 0;
}
cs

 

'Algorithm & Data structure > Problem Solution' 카테고리의 다른 글

앞자리가 같은 숫자들  (0) 2021.03.17
백준 - 1238. 파티  (0) 2020.10.10
백준 - 1762. 평면그래프와 삼각형  (4) 2020.05.16
알고리즘  (0) 2020.01.20
945. Minimum Increment to Make Array Unique  (0) 2019.12.29
Comments