홍시홍의 프로그래밍

[백준 4485] 젤다 본문

알고리즘 문제풀이/백준

[백준 4485] 젤다

홍시홍 2020. 1. 14. 00:24

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

 

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

문제 젤다의 전설 게임에서 화폐의 단위는 루피(rupee)다. 그런데 간혹 '도둑루피'라 불리는 검정색 루피도 존재하는데, 이걸 획득하면 오히려 소지한 루피가 감소하게 된다! 젤다의 전설 시리즈의 주인공, 링크는 지금 도둑루피만 가득한 N x N 크기의 동굴의 제일 왼쪽 위에 있다. [0][0]번 칸이기도 하다. 왜 이런 곳에 들어왔냐고 묻는다면 밖에서 사람들이 자꾸 "젤다의 전설에 나오는 녹색 애가 젤다지?"라고 물어봤기 때문이다. 링크가 녹색 옷을 입

www.acmicpc.net

 

요구사항

제일 오른쪽 밑에 도달하기 까지 최소의 금액을 잃는 경우 구하기

 

풀이

cost[][]를 무한으로 지정한다

도달할때 최소로 갱신이 된다면 진행하는 형태의 다익스트라 알고리즘을 사용한다.

 

#include <iostream>
#include <queue>

using namespace std;
int map[126][126];
int dist[126][126];
const int INF = 0x7fffffff;
int n;
int dc[4] = { 0,-1,0,1 };
int dr[4] = { -1,0,1,0 };
int main() 
{
	int cnt = 0;
	while (true) {
		cnt++;
		scanf("%d", &n);
		if (n == 0) {
			break;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				scanf("%d", &map[i][j]);
				dist[i][j] = INF;
			}
		}
		priority_queue<pair<int ,pair<int, int>>> q;
		q.push({ -map[0][0], {0,0} });
		dist[0][0] = map[0][0];
		while (!q.empty()) {
			int r = q.top().second.first;
			int c = q.top().second.second;
			int cost = -q.top().first;
			//cout << "R" << r << "C" << c << endl;
			q.pop();
			for (int i = 0; i < 4; i++) {
				int nr = r + dr[i];
				int nc = c + dc[i];
				if (nr < 0 || nc < 0 || nr >= n || nc >= n) {
					continue;
				}
				if (dist[nr][nc] > map[nr][nc] + cost) {
					dist[nr][nc] = map[nr][nc] + cost;
					//cout << "NR"<<nr << " " << nc << " " << -dist[nr][nc] << endl;
					
					q.push({ -dist[nr][nc],{nr,nc} });
				}
			}
		}
		printf("Problem %d: %d\n", cnt, dist[n - 1][n - 1]);
	}
}

'알고리즘 문제풀이 > 백준' 카테고리의 다른 글

[백준 11657] 타임 머신  (0) 2020.01.14
[백준 1504] 특정한 최단 경로  (0) 2020.01.14
[백준 1261] 알고스팟  (0) 2020.01.14
[백준 1238] 파티  (0) 2020.01.14
[백준 1987] 알파벳  (0) 2020.01.13
Comments