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

dh-winternagi 님의 블로그

(1520) 내리막 길 본문

백준 (C++)/Solve

(1520) 내리막 길

dh-winternagi 2026. 4. 19. 22:17

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

단계별로 풀어보기

31단계(동적 계획법 2) 3번째

 

 

 

DP와 그래프 순회(BFS, DFS)를 결합한 문제.

음... 잘 모르겠다. 개인적으로 이전 문제에 비하면 방법을 떠올리기도 어렵지 않았고 구현도 그리 어렵지 않았다.

 

 

 

#include <iostream>
#include <queue>
using namespace std;
typedef pair<int,int> p;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  
  int m, n;
  
  cin >> m >> n;
  
  vector v(m, vector<int> (n)), dp= v;
  priority_queue<p> pq;
  
  for(int i=0;i<m;i++){
    for(int j=0;j<n;j++){
      cin >> v[i][j];
    }
  }
  
  dp[0][0]= 1;
  pq.push({v[0][0], 0});
  
  while(!pq.empty()){
    int nowx= pq.top().second/n;
    int nowy= pq.top().second%n;
    int nowh= pq.top().first;
    pq.pop();
    
    for(int i=0;i<4;i++){
      int nextx= nowx+"0121"[i]-'1';
      int nexty= nowy+"1012"[i]-'1';
      
      if(nextx<0 || nextx>m-1 || nexty<0 || nexty>n-1)  continue;
      if(nowh<=v[nextx][nexty])  continue;
      
      if(!dp[nextx][nexty])  pq.push({v[nextx][nexty], nextx*n+nexty});
      dp[nextx][nexty]+= dp[nowx][nowy];
    }
  }
  
  cout << dp[m-1][n-1];
  
  return 0;
}

'백준 (C++) > Solve' 카테고리의 다른 글

(2293) 동전 1  (0) 2026.04.19
(2629) 양팔저울  (0) 2026.04.19
(11049) 행렬 곱셈 순서  (0) 2026.04.19
(11066) 파일 합치기  (0) 2026.04.19
(1450) 냅색문제  (0) 2026.04.19