-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_4485.java
More file actions
139 lines (113 loc) · 3.87 KB
/
Copy pathBOJ_4485.java
File metadata and controls
139 lines (113 loc) · 3.87 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.*;
import java.util.*;
/*
public class BOJ_4485 {
static int[] dr = {-1, 1, 0, 0};
static int[] dc = {0, 0, -1, 1};
static int[][] arr;
static boolean[][] visited;
static int result;
static int n;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
int idx = 0;
while(true) {
n = Integer.parseInt(br.readLine());
idx++;
if(n == 0) break;
arr = new int[n][n];
visited = new boolean[n][n];
for(int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < n; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
// System.out.println("arr = " + Arrays.deepToString(arr));
result = Integer.MAX_VALUE;
visited[0][0] = true;
backtracking(0, 0, arr[0][0], visited);
sb.append(String.format("Problem %d: %d", idx, result)).append("\n");
}
System.out.println(sb);
}
static void backtracking(int r, int c, int score, boolean[][] visited) {
if(r == n-1 && c == n-1) {
if(result > score) result = score;
return;
}
if(score > result) return;
for(int i = 0; i < 4; i++) {
int nr = r + dr[i], nc = c + dc[i];
if(nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if(!visited[nr][nc]) {
visited[nr][nc] = true;
backtracking(nr, nc, score+arr[nr][nc], visited);
visited[nr][nc] = false;
}
}
}
}
*/
public class BOJ_4485 {
static int[] dr = {-1, 1, 0, 0};
static int[] dc = {0, 0, -1, 1};
static int[][] arr;
static int[][] minScore;
static int n;
static class Node implements Comparable<Node> {
int r, c, score;
Node(int r, int c, int score) {
this.r = r;
this.c = c;
this.score = score;
}
@Override
public int compareTo(Node o) {
return this.score - o.score;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
int idx = 0;
while (true) {
n = Integer.parseInt(br.readLine());
if (n == 0) break;
idx++;
arr = new int[n][n];
minScore = new int[n][n];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
minScore[i][j] = Integer.MAX_VALUE;
}
}
dijkstra();
sb.append(String.format("Problem %d: %d", idx, minScore[n-1][n-1])).append("\n");
}
System.out.print(sb);
}
static void dijkstra() {
Queue<Node> q = new PriorityQueue<>();
minScore[0][0] = arr[0][0];
q.add(new Node(0, 0, arr[0][0]));
while (!q.isEmpty()) {
Node cur = q.poll();
if (cur.score > minScore[cur.r][cur.c]) continue;
for (int i = 0; i < 4; i++) {
int nr = cur.r + dr[i], nc = cur.c + dc[i];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
int newScore = cur.score + arr[nr][nc];
if (newScore < minScore[nr][nc]) {
minScore[nr][nc] = newScore;
q.add(new Node(nr, nc, newScore));
}
}
}
}
}