Skip to content

Commit 4d8b261

Browse files
committed
add
1 parent 969a3d3 commit 4d8b261

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

uva/1600/src/Main.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@ private void solveCase() throws IOException {
3838
int n = Integer.parseInt(st.nextToken());
3939
int k = Integer.parseInt(in.readLine());
4040
int[][] grid = new int[m][n];
41-
int[][] dists = new int[m][n];
41+
int[][][] dists = new int[m][n][k + 1];
4242
for (int i = 0; i < m; i++) {
4343
st = new StringTokenizer(in.readLine());
4444
for (int j = 0; j < n; j++) {
4545
grid[i][j] = Integer.parseInt(st.nextToken());
46-
dists[i][j] = Integer.MAX_VALUE;
46+
for (int v = 0; v < k + 1; v++) {
47+
dists[i][j][v] = Integer.MAX_VALUE;
48+
}
4749
}
4850
}
4951

5052

5153
Pos p1 = new Pos(0, 0, 0, k);
52-
dists[0][0] = 0;
54+
dists[0][0][k] = k;
5355

5456
PriorityQueue<Pos> pq = new PriorityQueue<>();
5557
pq.add(p1);
@@ -63,16 +65,22 @@ private void solveCase() throws IOException {
6365
for (int d = 0; d < 4; d++) {
6466
int ndx = p.x + dx[d];
6567
int ndy = p.y + dy[d];
66-
if (ndx >= 0 && ndx < m && ndy >= 0 && ndy < n && dists[ndx][ndy] > p.dist + 1) {
67-
if (grid[ndx][ndy] == 0) {
68-
dists[ndx][ndy] = p.dist + 1;
69-
Pos np = new Pos(ndx, ndy, p.dist + 1, k);
70-
pq.add(np);
71-
} else if (p.k > 0) {
72-
dists[ndx][ndy] = p.dist + 1;
73-
Pos np = new Pos(ndx, ndy, p.dist + 1, p.k - 1);
74-
pq.add(np);
75-
}
68+
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+
74+
}
75+
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);
83+
}
7684
}
7785
}
7886
}
@@ -89,6 +97,16 @@ private void solve() throws IOException {
8997
}
9098
}
9199

100+
class State {
101+
int dist;
102+
int k;
103+
104+
public State(int dist, int k) {
105+
this.dist = dist;
106+
this.k = k;
107+
}
108+
}
109+
92110
class Pos implements Comparable<Pos> {
93111
int x;
94112
int y;

0 commit comments

Comments
 (0)