์์ฆ 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)๊น์ง ์ต์ ๋น์ฉ์ผ๋ก ๊ฐ๋ ๋๊ฐ์ ํ์ด์ ๋ฌธ์ ์๋ค.
'Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SWEA - ์ด์ง์ ํํ (0) | 2023.02.05 |
---|---|
SWEA - ์๋ก์ด ๋ถ๋ฉด์ฆ ์น๋ฃ๋ฒ (0) | 2023.02.05 |
SWEA 5215๋ฒ - ํ๋ฒ๊ฑฐ ๋ค์ด์ดํธ (0) | 2022.08.11 |
SWEA 6808๋ฒ - ๊ท์์ด์ ์ธ์์ด์ ์นด๋๊ฒ์ (0) | 2022.08.09 |
SWEA 9229๋ฒ - ํ๋น์ด์ Spot Mart (0) | 2022.08.08 |