다익스트라 map활용연습

computer_log·2023년 9월 12일
0

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int N;
int map[3][3];
int sum = 0;
int direct[4][2] = {
	-1,0,
	1,0,
	0,-1,
	0,1
};
struct Node {
	int y,x;
	int weight;
};
const int MAX = 21e8;
int result[3][3];
bool operator<(Node v, Node t) {
	return t.weight < v.weight;
}
priority_queue<Node>q;
int main() {
	freopen_s(new FILE*, "input.txt", "r", stdin);
	cin >> N;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> map[i][j];
		}
	}
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			result[i][j] = MAX;
		}
	}
	q.push({ 0,0,map[0][0]});
	while (!q.empty()) {
		Node now = q.top();
		q.pop();
		if (result[now.y][now.x] < now.weight)continue;
		for (int t = 0; t < 4; t++) {
			int ny = now.y + direct[t][0];
			int nx = now.x + direct[t][1];
			if (nx < 0 || ny < 0 || ny >= 3 || nx >= 3)continue;
			int total = now.weight + map[ny][nx];

			if (result[ny][nx] > total) {
				result[ny][nx] = total;
				q.push({ ny,nx,total });
			}
		}
	}

	return 0;
}

#include <iostream>
#include <queue>
using namespace std;
int N;
int direct[4][2] = {
	-1,0,
	1,0,
	0,-1,
	0,1
};
int map[5][5];
int result[5][5];
const int MAX = 21e8;
struct Node {
	int y, x;
	int weight;
};
bool operator<(Node v, Node t) {
	return t.weight < v.weight;
}
priority_queue<Node>q;

int main() {
	freopen_s(new FILE*, "input.txt", "r", stdin);
	
		cin >> N;
		
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				cin >> map[i][j];
			}
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				result[i][j] = MAX;
			}
		}
		q.push({ 0,0,map[0][0] });
		while (!q.empty()) {
			Node now = q.top();
			q.pop();
			if (result[now.y][now.x] < now.weight)continue;
			for (int t = 0; t < 4; t++) {
				int ny = now.y + direct[t][0];
				int nx = now.x + direct[t][1];
				if (ny < 0 || nx < 0 || ny >= 5 || nx >= 5)continue;
				int total = map[ny][nx] + now.weight;
				if (result[ny][nx] > total) {
					result[ny][nx] = total;
					q.push({ ny,nx,total });
				}
			}
		}
		cout << result[N - 1][N - 1];
	return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int N;
int direct[4][2] = {
	-1,0,
	1,0,
	0,-1,
	0,1
};
int map[126][126];
int result[126][126];
const int MAX = 21e8;
struct Node {
	int y, x;
	int weight;
};
bool operator<(Node v, Node t) {
	return t.weight < v.weight;
}
priority_queue<Node>q;
int cnt = 1;
int main() {
	//freopen_s(new FILE*, "input.txt", "r", stdin);
	while (1) {
		cin >> N;

		if (N == 0)break;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				cin >> map[i][j];
			}
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				result[i][j] = MAX;
			}
		}
		q.push({ 0,0,map[0][0] });
		while (!q.empty()) {
			Node now = q.top();
			q.pop();
			if (result[now.y][now.x] < now.weight)continue;
			for (int t = 0; t < 4; t++) {
				int ny = now.y + direct[t][0];
				int nx = now.x + direct[t][1];
				if (ny < 0 || nx < 0 || ny >= N || nx >= N)continue;
				int total = map[ny][nx] + now.weight;
				if (result[ny][nx] > total) {
					result[ny][nx] = total;
					q.push({ ny,nx,total });
				}
			}
		}
		cout << "Problem " <<cnt++<<": "<<result[N - 1][N - 1] << "\n";
	}
	return 0;
}
profile
computer_log

0개의 댓글