@@ -38,18 +38,20 @@ private void solveCase() throws IOException {
38
38
int n = Integer .parseInt (st .nextToken ());
39
39
int k = Integer .parseInt (in .readLine ());
40
40
int [][] grid = new int [m ][n ];
41
- int [][] dists = new int [m ][n ];
41
+ int [][][] dists = new int [m ][n ][ k + 1 ];
42
42
for (int i = 0 ; i < m ; i ++) {
43
43
st = new StringTokenizer (in .readLine ());
44
44
for (int j = 0 ; j < n ; j ++) {
45
45
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
+ }
47
49
}
48
50
}
49
51
50
52
51
53
Pos p1 = new Pos (0 , 0 , 0 , k );
52
- dists [0 ][0 ] = 0 ;
54
+ dists [0 ][0 ][ k ] = k ;
53
55
54
56
PriorityQueue <Pos > pq = new PriorityQueue <>();
55
57
pq .add (p1 );
@@ -63,16 +65,22 @@ private void solveCase() throws IOException {
63
65
for (int d = 0 ; d < 4 ; d ++) {
64
66
int ndx = p .x + dx [d ];
65
67
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
+ }
76
84
}
77
85
}
78
86
}
@@ -89,6 +97,16 @@ private void solve() throws IOException {
89
97
}
90
98
}
91
99
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
+
92
110
class Pos implements Comparable <Pos > {
93
111
int x ;
94
112
int y ;
0 commit comments