백준 4486번 녹색 옷을 입은 애가 젤다지?
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 | #include <iostream> #include<vector> #include<queue> #include<string> #include<cstring> #include<algorithm> #include<cstring> using namespace std; int arr[126][126]; int n; int dy[4] = { -1,0,1,0 }; int dx[4] = { 0,1,0,-1 }; bool visited[126][126]; int route() { priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq; pq.push({ arr[0][0],{0,0} }); visited[0][0] = 1; while (!pq.empty()) { int y = pq.top().second.first; int x = pq.top().second.second; int cost = pq.top().first; pq.pop(); if (y == n - 1 && x == n - 1) return cost; for (int i = 0; i < 4; i++) { int nexty = y + dy[i]; int nextx = x + dx[i]; if (0 <= nexty && nexty < n && 0 <= nextx && nextx < n) { if (!visited[nexty][nextx]) { pq.push({ cost + arr[nexty][nextx],{nexty,nextx} }); visited[nexty][nextx] = 1; } } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int cnt = 1; do { memset(visited, 0, sizeof(visited)); cin >> n; if (!n) return 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> arr[i][j]; cout << "Problem " << cnt++ << ": " << route() << endl; } while (n); return 0; } | cs |
출처:https://www.acmicpc.net/problem/4485
ㅋㅋ 문제내용을 너무 재밌게 쓰셧네요. 난이도 자체는 매우 쉬운편이였습니다.
우선 순위큐를 활용해서 쉽게 풀 수 있는 문제였네요. 주석을 따로 달지 않아도 될 것 같습니다.
'알고리즘 > BAEKJOON' 카테고리의 다른 글
| 백준 9370번 미확인 도착지 (0) | 2019.05.15 |
|---|---|
| 백준 1504번 특정한 최단 경로 (0) | 2019.05.15 |
| 백준 1261번 알고스팟 (0) | 2019.05.12 |
| 백준 1238번 파티 (0) | 2019.05.11 |
| 백준 12115번 Baza (0) | 2019.05.11 |