반응형

 

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

 

1890번: 점프

첫째 줄에 게임 판의 크기 N (4 ≤ N ≤ 100)이 주어진다. 그 다음 N개 줄에는 각 칸에 적혀져 있는 수가 N개씩 주어진다. 칸에 적혀있는 수는 0보다 크거나 같고, 9보다 작거나 같은 정수이며, 가장

www.acmicpc.net

DP 써야하는 이유

Priority Queue 사용한 이유

(1, 2)가 3이 된 상태에서 (1, 3)과 (2, 2)를 업데이트 해야 하는데

(1, 2)가 1일 때 (1, 3)과 (2, 2)를 업데이트 한 뒤에 (1, 2)가 3이 되면 값이 이상해진다.

 

반례

4

1 1 1 1

2 1 1 1

1 1 1 1

1 1 1 0

 

 

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
#include <iostream>
#include <queue>
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
using namespace std;
struct Node {
    int x;
    int y;
};
struct cmp {
    bool operator()(Node a, Node b) {
        return a.x + a.y > b.x + b.y;
    }
};
int n, arr[101][101];
int dx[] = { 10 };
int dy[] = { 01 };
bool visited[101][101];
ll dp[101][101];
void func() {
    priority_queue<Node, vector<Node>, cmp> q;
    q.push({ 0,0 });
    dp[0][0= 1;
    visited[0][0= 1;
    while (!q.empty()) {
        Node now = q.top(); q.pop();
        int x = now.x;
        int y = now.y;
        if (arr[x][y] == 0continue;
        rep(i, 2) {
            int nx = x + dx[i] * arr[x][y];
            int ny = y + dy[i] * arr[x][y];
            if (nx >= n || ny >= n) continue;
            if (!visited[nx][ny]) {
                visited[nx][ny] = 1;
                q.push({ nx, ny });
            }
            dp[nx][ny] += dp[x][y];
        }
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    rep(i, n) {
        rep(j, n)
            cin >> arr[i][j];
    }
    func();
    cout << dp[n - 1][n - 1];
}
cs
반응형

'백준 > DP' 카테고리의 다른 글

백준 11066  (0) 2021.07.27
백준 1937  (0) 2021.07.26
백준 2293  (0) 2021.07.20
백준 1103  (0) 2021.07.08
백준 11054  (0) 2021.02.19

+ Recent posts