티스토리 뷰
//알고리즘
1. 다익스트라
2. 전에 우선순위 큐를 사용해서 구현한 문제
3. 시작점과 끝점이 정해져있고 최소거리를 구하면 되기 때문에 다익스트라로 구현.
package swexpertacademy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class 보급로_다익스트라 {
static int T, N, map[][], Ans;
static boolean[][] v;
static int[] dr = { -1, 1, 0, 0 };
static int[] dc = { 0, 0, -1, 1 };
static class Point {
int r, c, cnt;
public Point(int r, int c, int cnt) {
super();
this.r = r;
this.c = c;
this.cnt = cnt;
}
public Point(int r, int c) {
super();
this.r = r;
this.c = c;
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
N = Integer.parseInt(br.readLine());
map = new int[N][N];
v = new boolean[N][N];
for (int i = 0; i < map.length; i++) {
String str = br.readLine();
for (int j = 0; j < map[i].length; j++) {
map[i][j] = str.charAt(j) - '0';
}
}
Ans = Integer.MAX_VALUE;
dijkstra(0, 0);
System.out.printf("#%d %d\n", tc, Ans);
}
}
private static void dijkstra(int r, int c) {
int[][] dist = new int[N][N];
for (int i = 0; i < dist.length; i++) {
Arrays.fill(dist[i], Integer.MAX_VALUE);
}
dist[r][c] = 0;
PriorityQueue<Point> q = new PriorityQueue<Point>((o1, o2) -> (Integer.compare(o1.cnt, o2.cnt)));
q.add(new Point(r, c, 0));
Point p = null;
while (!q.isEmpty()) {
p = q.poll();
if (v[p.r][p.c])
continue;
v[p.r][p.c] = true;
for (int k = 0; k < 4; k++) {
int nr = p.r + dr[k];
int nc = p.c + dc[k];
if (check(nr, nc))
continue;
if (!v[nr][nc] && dist[nr][nc] > map[nr][nc] + p.cnt) {
dist[nr][nc] = map[nr][nc] + p.cnt;
q.add(new Point(nr, nc, dist[nr][nc]));
}
}
}
Ans = dist[N - 1][N - 1];
}
private static boolean check(int nr, int nc) {
if (nr < 0 || nc < 0 || nr >= N || nc >= N)
return true;
return false;
}
}
'Algorithm' 카테고리의 다른 글
백준_치즈_2636 (0) | 2020.10.27 |
---|---|
백준_마법사 상어와 토네이도_20057 (0) | 2020.10.26 |
백준_마법사상어와파이어볼_20056 (0) | 2020.10.24 |
백준_게리맨더링_17471 (0) | 2020.10.23 |
백준_z_1074 (0) | 2020.10.22 |