티스토리 뷰

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

 

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

젤다의 전설 게임에서 화폐의 단위는 루피(rupee)다. 그런데 간혹 '도둑루피'라 불리는 검정색 루피도 존재하는데, 이걸 획득하면 오히려 소지한 루피가 감소하게 된다! 젤다의 전설 시리즈의 주��

www.acmicpc.net

백준 소스코드 [C++] 4485 녹색 옷 입은 애가 젤다지

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#define MAX 987654321
using namespace std;
int arr[127][127], n, cnt = 0;
int dist[127][127];
pair<int, int> pp, p[4] = { {1,0}, {0,1}, {-1,0}, {0,-1} };
queue <pair<int, int> > q;
void bfs() {
	dist[1][1] = arr[1][1];
	while (q.empty() != true) {
		pp = q.front();
		q.pop();
		for (int i = 0;i < 4;i++) {
			if (dist[pp.first + p[i].first][pp.second + p[i].second] > dist[pp.first][pp.second]
				+arr[pp.first + p[i].first][pp.second + p[i].second]) {
				q.push({ pp.first + p[i].first,pp.second + p[i].second });
				dist[pp.first + p[i].first][pp.second + p[i].second] = dist[pp.first][pp.second]
					+ arr[pp.first + p[i].first][pp.second + p[i].second];
			}
		}
	}
	cout << "Problem " << ++cnt << ": " << dist[n][n] << '\n';
	return;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	while (true) {
		cin >> n;
		if (n == 0) break;
		for (int i = 0;i <= n + 1;i++)for (int j = 0;j <= n + 1;j++) {
			arr[i][j] = MAX;
			dist[i][j] = MAX;
		}
		for (int i = 1;i <= n;i++) for (int j = 1;j <= n;j++) cin >> arr[i][j];
		q.push({ 1,1 });
		bfs();
	}
	return 0;
}
댓글