문제를 살펴보면 딱히 어려운 문제는 아니다. 

그냥 다익스트라 2차원으로 돌리면된다. 

흰색이 1 검은색이 0이라는 점에 착안하면 검은색에 가중치를 두기 위해 필자는 원본을 toggle 해줬다. 

그냥 벡터도 필요없이 dst 2차원을 갱신해주며 다익스트라를 돌려주자
(요즘 너무 쉬운것만 올리는것 같다 - 맨날 이소리하고 맨날 쉬운거 올린다)

https://www.acmicpc.net/problem/2665

<Code>

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
// pda_pro12
#include<iostream>
#include<algorithm>
#include<queue>
#include<stdio.h>
 
#define INT 0x7fffffff
#define N_ 51
 
using namespace std;
 
int n, a[N_][N_], dst[N_][N_], dx[] = { -1,1,0,0 }, dy[] = { 0,0,1,-1 };
priority_queue<pair<pair<intint>int>> pq;
 
int main() {
    cin >> n;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            scanf("%1d"&a[i][j]);
            a[i][j] = (a[i][j] == 0) ? 1 : 0
            dst[i][j] = INT;
        }
    }
    dst[0][0= 0;
    pq.push({ {0,0},0 });
    while (!pq.empty()) {
        int v_x = pq.top().first.first; int v_y = pq.top().first.second;
        int dis = -pq.top().second;
 
        pq.pop();
        if (dis > dst[v_x][v_y]) continue;
        for (int i = 0; i < 4; i++) {
            int nx = v_x + dx[i]; int ny = v_y + dy[i]; 
            if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue
            int n_dis = dis + a[nx][ny]; 
            if (n_dis < dst[nx][ny]) {
                dst[nx][ny] = n_dis; 
                pq.push({ {nx, ny}, -n_dis });
            }
        }
    }
    cout << dst[n - 1][n - 1]; 
    return 0;
}


'PS) BOJ > Disjoint_set' 카테고리의 다른 글

[C++] 네트워크 연결 [백준 3780]  (0) 2018.08.17
[C++] 공항 [BOJ 10775]  (0) 2018.08.17

+ Recent posts