๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€?

by seolhee2750 2022. 8. 31.
๋ฌธ์ œ

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

 

4485๋ฒˆ: ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€?

์ ค๋‹ค์˜ ์ „์„ค ๊ฒŒ์ž„์—์„œ ํ™”ํ์˜ ๋‹จ์œ„๋Š” ๋ฃจํ”ผ(rupee)๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ฐ„ํ˜น '๋„๋‘‘๋ฃจํ”ผ'๋ผ ๋ถˆ๋ฆฌ๋Š” ๊ฒ€์ •์ƒ‰ ๋ฃจํ”ผ๋„ ์กด์žฌํ•˜๋Š”๋ฐ, ์ด๊ฑธ ํš๋“ํ•˜๋ฉด ์˜คํžˆ๋ ค ์†Œ์ง€ํ•œ ๋ฃจํ”ผ๊ฐ€ ๊ฐ์†Œํ•˜๊ฒŒ ๋œ๋‹ค! ์ ค๋‹ค์˜ ์ „์„ค ์‹œ๋ฆฌ์ฆˆ์˜ ์ฃผ

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class BOJ_4485 {

	static class Node implements Comparable<Node> {
		int x;
		int y;
		int w;

		public Node(int x, int y, int w) {
			this.x = x;
			this.y = y;
			this.w = w;
		}

		@Override
		public int compareTo(Node o) {
			return Integer.compare(this.w, o.w);
		}
	}

	static int n, idx;
	static int[][] map;
	static int[][] dis;
	static int[] dx = {0, 0, -1, 1};
	static int[] dy = {-1, 1, 0, 0};

	public static void Dijk() {
		PriorityQueue<Node> pq = new PriorityQueue<>();
		pq.add(new Node(0, 0, map[0][0])); // x, y, w
		dis[0][0] = map[0][0];

		while(!pq.isEmpty()) {
			Node now = pq.poll();
			int x = now.x;
			int y = now.y;
			int w = now.w;

			for(int i = 0; i < 4; i++) {
				int nx = x + dx[i];
				int ny = y + dy[i];

				if(nx < 0 || ny < 0 || nx >= n || ny >= n || dis[nx][ny] <= map[nx][ny] + w) continue;

				dis[nx][ny] = map[nx][ny] + w;
				pq.add(new Node(nx, ny, dis[nx][ny]));
			}
		}
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder out = new StringBuilder();

		while(true) {
			idx++;
			n = Integer.parseInt(in.readLine());
			if(n == 0) break;

			map = new int[n][n];
			dis = new int[n][n];
			for(int i = 0; i < n; i++) {
				st = new StringTokenizer(in.readLine());
				for(int j = 0; j < n; j++) {
					map[i][j] = Integer.parseInt(st.nextToken());
					dis[i][j] = Integer.MAX_VALUE;
				}
			}

			Dijk();
			out.append("Problem " + idx + ": " + dis[n-1][n-1] + "\n");
		}

		System.out.print(out);
	}
}

๐Ÿ‘‰ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ

  • Node ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ๋„๋‘‘๋ฃจํ”ผ์˜ ๊ฐ’ w ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค.
  • ์ž…๋ ฅ๋ฐ›๋Š” ๋™๊ตด ์ •๋ณด๋ฅผ map 2์ฐจ์› ๋ฐฐ์—ด์— ์ €์žฅํ–ˆ๊ณ ,
    dis ๋ฐฐ์—ด์€ map๊ณผ ๊ฐ™์€ ํฌ๊ธฐ๋กœ ํ•˜๋˜ ๋ชจ๋“  ์นธ์„ MAX_VALUE๋กœ ์ฑ„์›Œ์ฃผ์—ˆ๋‹ค. (์ตœ์†Ÿ๊ฐ’ ๊ฐฑ์‹  ์œ„ํ•ด)
  • Dijk ๋ฉ”์„œ๋“œ์—์„œ๋Š” ์ตœ์†Ÿ ๊ฐ’์„ ๊ฐ€์žฅ ํšจ์œจ์ ์œผ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋„๋ก PriorityQueue๋ฅผ ๋งŒ๋“ค์—ˆ๊ณ 
    ํƒ์ƒ‰ ์‹œ์ž‘์ ์œผ๋กœ 0, 0 ์ขŒํ‘œ์™€ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ๋„๋‘‘๋ฃจํ”ผ ๊ฐ’์„ pq์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
  • while์€ ๋ณดํŽธ์ ์ธ bfs ํƒ์ƒ‰๊ณผ ๊ฐ™๋‹ค.
    pq์˜ ๋งจ ์•ž์—์„œ ๊ฐ’์„ ํ•˜๋‚˜ ๋นผ์˜ค๊ณ , ๊ทธ ๊ฐ’์„ ํ† ๋Œ€๋กœ ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰์„ ํ•ด์ฃผ์—ˆ๋‹ค.
    dis[nx][ny] <= map[nx][ny] + w ์ด๋ฉด ํ•ด๋‹น ๊ฒฝ๋กœ๋กœ๋Š” ๋”์ด์ƒ ์ตœ์†Ÿ๊ฐ’์ผ ๊ฐ€๋Šฅ์„ฑ์ด ์—†๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ,
    ์œ„์˜ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” dis ๋ฐฐ์—ด์„ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , pq์—๋„ ์ƒˆ๋กœ์šด ํƒ์ƒ‰ ์‹œ์ž‘์ ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.
  • Dijk ๋ฉ”์„œ๋“œ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด dis ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์นธ์— ์ €์žฅ๋œ ๊ฐ’์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • PriorityQueue๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ์ถฉ๋ถ„ํžˆ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ,
    ์‚ฌ์šฉํ•˜๊ณ  ์•ˆํ•˜๊ณ ์˜ ์ฐจ์ด๊ฐ€ ๊ฑฐ์˜ ๋‘ ๋ฐฐ ์‹œ๊ฐ„ ์ฐจ์ด๊ฐ€ ๋‚˜๋”๋ผ!!!!

๋Œ“๊ธ€