Skip to content

Commit dafdb3a

Browse files
committed
add
1 parent 4d8b261 commit dafdb3a

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

uva/1600/src/Main.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import java.io.IOException;
33
import java.io.InputStreamReader;
44
import java.io.PrintWriter;
5+
import java.util.ArrayList;
56
import java.util.PriorityQueue;
67
import java.util.StringTokenizer;
78
import java.util.stream.IntStream;
@@ -38,20 +39,17 @@ private void solveCase() throws IOException {
3839
int n = Integer.parseInt(st.nextToken());
3940
int k = Integer.parseInt(in.readLine());
4041
int[][] grid = new int[m][n];
41-
int[][][] dists = new int[m][n][k + 1];
42+
ArrayList<State>[][] ss = new ArrayList[m][n];
4243
for (int i = 0; i < m; i++) {
4344
st = new StringTokenizer(in.readLine());
4445
for (int j = 0; j < n; j++) {
4546
grid[i][j] = Integer.parseInt(st.nextToken());
46-
for (int v = 0; v < k + 1; v++) {
47-
dists[i][j][v] = Integer.MAX_VALUE;
48-
}
47+
ss[i][j] = new ArrayList<>();
4948
}
5049
}
5150

52-
5351
Pos p1 = new Pos(0, 0, 0, k);
54-
dists[0][0][k] = k;
52+
ss[0][0].add(new State(0, k));
5553

5654
PriorityQueue<Pos> pq = new PriorityQueue<>();
5755
pq.add(p1);
@@ -66,21 +64,23 @@ private void solveCase() throws IOException {
6664
int ndx = p.x + dx[d];
6765
int ndy = p.y + dy[d];
6866
if (ndx >= 0 && ndx < m && ndy >= 0 && ndy < n) {
69-
if (dists[ndx][ndy].dist > p.dist + 1 || dists[ndx][ndy].k)
70-
71-
if (grid[ndx][ndy] == 0) {
72-
if (dists[ndx][ndy].dist > p.dist + 1 || dists[ndx][ndy].k < k) {
73-
67+
int newDist = p.dist + 1;
68+
int newK = grid[ndx][ndy] == 1 ? p.k - 1 : k;
69+
70+
if (newK >= 0) {
71+
boolean shouldUpdate = true;
72+
for (State state : ss[ndx][ndy]) {
73+
if (state.dist <= newDist && state.k >= newK) {
74+
shouldUpdate = false;
75+
break;
7476
}
77+
}
7578

76-
dists[ndx][ndy] = p.dist + 1;
77-
Pos np = new Pos(ndx, ndy, p.dist + 1, k);
78-
pq.add(np);
79-
} else if (p.k > 0) {
80-
dists[ndx][ndy] = p.dist + 1;
81-
Pos np = new Pos(ndx, ndy, p.dist + 1, p.k - 1);
82-
pq.add(np);
79+
if (shouldUpdate) {
80+
ss[ndx][ndy].add(new State(newDist, newK));
81+
pq.add(new Pos(ndx, ndy, newDist, newK));
8382
}
83+
}
8484
}
8585
}
8686
}
@@ -89,6 +89,7 @@ private void solveCase() throws IOException {
8989
out.println(ans);
9090
}
9191

92+
9293
private void solve() throws IOException {
9394
int c = Integer.parseInt(in.readLine());
9495
for (int i = 0; i < c; i++) {

0 commit comments

Comments
 (0)