https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15QRX6APsCFAYD&categoryId=AV15QRX6APsCFAYD&categoryType=CODE&problemTitle=1249&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

 

 

 

 

์š”์ฆ˜ DP๋ฅผ ํ’€๊ณ  ์žˆ์–ด์„œ ๊ทธ๋Ÿฐ์ง€ ์ด ๋ฌธ์ œ๋„ ๋ณด๊ณ ์„œ DP๋กœ ํ’€๋ฉด ์ข‹๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ DP๋กœ ํ’€์—ˆ๋Š”๋ฐ ๋ชจ๋“  ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.. ์ด์œ ๋ฅผ ์•Œ๊ณ  ๋ณด๋‹ˆ ๋ฌธ์ œ์—์„œ '์ƒํ•˜์ขŒ์šฐ'๋กœ ์›€์ง์ธ๋‹ค๊ณ  ํ–ˆ๋‹ค. ๋งŒ์•ฝ '์šฐํ•˜'๋กœ๋งŒ ์›€์ง์˜€๋‹ค๋ฉด DP๋กœ ํ‘ธ๋Š” ํ’€์ด๊ฐ€ ํ†ตํ–ˆ์„ ๊ฒƒ์ด๋‹ค.

 

 

 

 

์‹œ๋„ - DP

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class swea_๋ณด๊ธ‰๋กœdp {

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int T = Integer.parseInt(br.readLine()); //ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜
		
		for(int t = 1; t <= T; t++) {
			int N = Integer.parseInt(br.readLine());
			int[][] graph = new int[N][N];
			
			for(int i = 0; i < N; i++) {
				String input = br.readLine();
				
				for(int j = 0; j < N; j++) {
					graph[i][j] = input.charAt(j) - '0';				
				}
			}
			
			//๊ตฌํ˜„
			int[][] dp = new int[N][N];
			
			for(int i = 1; i < N; i++) {
				dp[i][0] = dp[i - 1][0] + graph[i][0];
			}
			
			for(int i = 1; i < N; i++) {
				dp[0][i] = dp[0][i - 1] + graph[0][i];
			}
			
			for(int i = 1; i < N; i++) {
				for(int j = 1; j < N; j++) {
					dp[i][j] = graph[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
				}
			}
			
			//์ถœ๋ ฅ
			int answer = dp[N - 1][N - 1];
			
			System.out.println("#" +  t + " " + answer);
		}
	}

}

 

 

 

 

 

 

์ •๋‹ต ์ฝ”๋“œ - ๋‹ค์ต์ŠคํŠธ๋ผ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;

public class Solution {
	static int N;
	static int[][] graph;
	static boolean[][] visited;
	static int[][] dist;
	static int[] dx = {-1, 1, 0, 0};
	static int[] dy = {0, 0, -1, 1};
	
	static class Dot implements Comparable<Dot> {
		int x, y, time;

		public Dot(int x, int y, int time) {
			this.x = x;
			this.y = y;
			this.time = time;
		}

		@Override
		public int compareTo(Dot o) {
			return this.time - o.time;
		}
	}
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int T = Integer.parseInt(br.readLine()); //ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜
		
		for(int t = 1; t <= T; t++) {
			N = Integer.parseInt(br.readLine());
			graph = new int[N][N];
			
			for(int i = 0; i < N; i++) {
				String input = br.readLine();
				
				for(int j = 0; j < N; j++) {
					graph[i][j] = input.charAt(j) - '0';				
				}
			}
			
			//๊ตฌํ˜„
			visited = new boolean[N][N];
			
			dist = new int[N][N];
			for(int d[] : dist) { //๊ฑฐ๋ฆฌ ๋ฌดํ•œ์œผ๋กœ ์ดˆ๊ธฐํ™”
				Arrays.fill(d, Integer.MAX_VALUE);
			}
			
			dijkstra(); //๋‹ค์ต์ŠคํŠธ๋ผ
			
			//์ถœ๋ ฅ
			System.out.println("#" +  t + " " + dist[N-1][N-1]);
		}
	}

	public static void dijkstra() {
		PriorityQueue<Dot> queue = new PriorityQueue<>();
		
		queue.add(new Dot(0, 0, graph[0][0])); //์‹œ์ž‘ ์ขŒํ‘œ
		dist[0][0] = graph[0][0];
		
		while(!queue.isEmpty()) {
			Dot dot = queue.poll();
			
			if(dot.x == N - 1 && dot.y == N - 1) { //์ข…๋ฃŒ ์กฐ๊ฑด
				return;
			}
			
			if(visited[dot.x][dot.y]) continue;
			
			visited[dot.x][dot.y] = true;
			
			for(int i = 0; i < 4; i++) {
				int nx = dot.x + dx[i];
				int ny = dot.y + dy[i];
				
				if(0 <= nx && nx < N && 0 <= ny && ny < N) {
					if(dist[dot.x][dot.y] + graph[nx][ny] < dist[nx][ny]) { //๋” ์ตœ์†Œ์ธ ๊ณณ์ด ์žˆ์œผ๋ฉด
						dist[nx][ny] = dist[dot.x][dot.y] + graph[nx][ny];
						queue.add(new Dot(nx, ny, dist[nx][ny]));
					}
				}
			}
		}
	}
	
}

 

 

 

 

 

์ •๋‹ต

 

 

 

 

 

๋ฐฑ์ค€์˜ '๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€?'์™€ ๊ฐ™์ด ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์จ์„œ (0,0)์—์„œ (n-1, n-1)๊นŒ์ง€ ์ตœ์†Œ ๋น„์šฉ์œผ๋กœ ๊ฐ€๋Š” ๋˜‘๊ฐ™์€ ํ’€์ด์˜ ๋ฌธ์ œ์˜€๋‹ค.

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

 

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

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

www.acmicpc.net

giraffe_