-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1149BFS.java
More file actions
71 lines (62 loc) · 1.9 KB
/
P1149BFS.java
File metadata and controls
71 lines (62 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package quki.algorithm.dp;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/**
* BOJ #1149 Using BFS
*
* @author quki
*/
public class P1149BFS {
public static class Point {
int i;
int j;
Point(int i, int j) {
this.i = i;
this.j = j;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n][3];
int[][] s = new int[n][3]; // 해당 index까지의 sum의 최소값
int inf = 1000000000;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
int e = sc.nextInt();
;
a[i][j] = e;
if (i == 0) {
s[i][j] = e;
} else {
s[i][j] = inf;
}
}
}
// BFS
Queue<Point> q = new LinkedList<>();
int i = 0;
int min = inf;
for (int j = 0; j < 3; j++) {
q.add(new Point(i, j));
while (!q.isEmpty()) {
Point p = q.poll();
int newI = p.i + 1; // 한 줄 아래 row
for (int k = 0; k < 3; k++) {
int newJ = k; // k -> 0,1,2
if (newI <= n - 1 && p.j != newJ) { // 바로 아래칸 제외 (같은 column)
q.add(new Point(newI, newJ));
if (s[newI][newJ] > s[newI - 1][p.j] + a[newI][newJ]) {
s[newI][newJ] = s[newI - 1][p.j] + a[newI][newJ]; // 최소값으로 초기화
if (newI == n - 1) { // 맨 아래 row
min = Math.min(s[newI][newJ], min);
}
}
}
}
}
}
System.out.println(min);
}
}