3345
|
1 /*
|
|
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 *
|
|
5 * This code is free software; you can redistribute it and/or modify it
|
|
6 * under the terms of the GNU General Public License version 2 only, as
|
|
7 * published by the Free Software Foundation.
|
|
8 *
|
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 * version 2 for more details (a copy is included in the LICENSE file that
|
|
13 * accompanied this code).
|
|
14 *
|
|
15 * You should have received a copy of the GNU General Public License version
|
|
16 * 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 *
|
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 * or visit www.oracle.com if you need additional information or have any
|
|
21 * questions.
|
|
22 *
|
|
23 */
|
|
24
|
|
25 /**
|
|
26 * @test
|
|
27 * @bug 6890943
|
|
28 * @summary JVM mysteriously gives wrong result on 64-bit 1.6 VMs in hotspot mode.
|
|
29 *
|
|
30 * @run shell Test6890943.sh
|
|
31 */
|
|
32 import java.util.*;
|
|
33 import java.io.*;
|
|
34 import java.util.regex.*;
|
|
35
|
|
36 public class Test6890943 {
|
|
37 public static final boolean AIR = true, ROCK = false;
|
|
38 public static void main(String[] args) {
|
|
39 new Test6890943().go();
|
|
40 }
|
|
41
|
|
42 int r, c, f, t;
|
|
43 boolean[][] grid;
|
|
44
|
|
45 public void go() {
|
|
46 Scanner s = new Scanner(System.in);
|
|
47 s.useDelimiter("\\s+");
|
|
48 int T = s.nextInt();
|
|
49 for (t = 0 ; t < T ; t++) {
|
|
50 r = s.nextInt(); c = s.nextInt(); f = s.nextInt();
|
|
51 grid = new boolean[r][c];
|
|
52 for (int x = 0 ; x < r ; x++) {
|
|
53 String line = s.next();
|
|
54 for (int y = 0 ; y < c ; y++) grid[x][y] = line.charAt(y) == '.';
|
|
55 }
|
|
56 int digs = solve();
|
|
57 String res = digs == -1 ? "No" : "Yes " + digs;
|
|
58 System.out.printf("Case #%d: %s\n", t+1, res);
|
|
59 }
|
|
60 }
|
|
61
|
|
62 Map<Integer, Integer> M = new HashMap<Integer, Integer>();
|
|
63
|
|
64 private int solve() {
|
|
65 M = new HashMap<Integer, Integer>();
|
|
66 M.put(calcWalkingRange(0, 0), 0);
|
|
67 for (int digDown = 0 ; digDown < r ; digDown++) {
|
|
68 Map<Integer, Integer> tries = new HashMap<Integer, Integer>();
|
|
69 for (Map.Entry<Integer, Integer> m : M.entrySet()) {
|
|
70 int q = m.getKey();
|
|
71 if (depth(q) != (digDown)) continue;
|
|
72 if (stuck(q)) continue;
|
|
73 tries.put(q, m.getValue());
|
|
74 }
|
|
75
|
|
76 for (Map.Entry<Integer, Integer> m : tries.entrySet()) {
|
|
77 int q = m.getKey();
|
|
78 int fallLeftDelta = 0, fallRightDelta = 0;
|
|
79 //fall left
|
|
80 int fallLeft = fall(digDown, start(q));
|
|
81 if (fallLeft > 0) {
|
|
82 fallLeftDelta = 1;
|
|
83 if (fallLeft <= f) addToM(calcWalkingRange(digDown+fallLeft, start(q)), m.getValue());
|
|
84 }
|
|
85
|
|
86 //fall right
|
|
87 int fallRight = fall(digDown, end(q));
|
|
88 if (fallRight > 0) {
|
|
89 fallRightDelta = 1;
|
|
90
|
|
91 if (fallRight <= f) addToM(calcWalkingRange(digDown+fallRight, end(q)), m.getValue());
|
|
92 }
|
|
93
|
|
94 for (int p = start(q) + fallLeftDelta ; p <= end(q) - fallRightDelta ; p++) {
|
|
95 //goLeft
|
|
96 for (int digSpot = p ; digSpot > start(q) +fallLeftDelta ; digSpot--) {
|
|
97 int fallDown = 1+fall(digDown+1, digSpot);
|
|
98 if (fallDown <= f) {
|
|
99 if (fallDown == 1) {
|
|
100 addToM(calcWalkingRange(digDown + 1, digSpot, digSpot, p), m.getValue() + Math.abs(digSpot-p)+1);
|
|
101 } else {
|
|
102 addToM(calcWalkingRange(digDown + fallDown, digSpot), m.getValue() + Math.abs(digSpot-p)+1);
|
|
103 }
|
|
104 }
|
|
105 }
|
|
106
|
|
107 //goRight
|
|
108 for (int digSpot = p ; digSpot < end(q)-fallRightDelta ;digSpot++) {
|
|
109 int fallDown = 1+fall(digDown+1, digSpot);
|
|
110 if (fallDown <= f) {
|
|
111 if (fallDown == 1) {
|
|
112 addToM(calcWalkingRange(digDown + 1, digSpot, p, digSpot), m.getValue() + Math.abs(digSpot-p)+1);
|
|
113 } else {
|
|
114 addToM(calcWalkingRange(digDown + fallDown, digSpot), m.getValue() + Math.abs(digSpot-p)+1);
|
|
115 }
|
|
116 }
|
|
117 }
|
|
118 }
|
|
119 }
|
|
120 }
|
|
121
|
|
122 int result = Integer.MAX_VALUE;
|
|
123 for (Map.Entry<Integer, Integer> m : M.entrySet()) {
|
|
124 if (depth(m.getKey()) == r-1) result = Math.min(m.getValue(), result);
|
|
125 }
|
|
126
|
|
127 if (result == Integer.MAX_VALUE) return -1;
|
|
128 return result;
|
|
129 }
|
|
130
|
|
131 private void addToM(int q, int i) {
|
|
132 Integer original = M.get(q);
|
|
133 if ( original == null ) M.put(q, i);
|
|
134 else M.put(q, Math.min(original, i));
|
|
135 }
|
|
136
|
|
137 private int fall(int row, int column) {
|
|
138 int res = 0;
|
|
139 for ( int p = row+1 ; p < r ; p++) {
|
|
140 if (grid[p][column] == AIR) res++;
|
|
141 else break;
|
|
142 }
|
|
143 return res;
|
|
144 }
|
|
145
|
|
146 private boolean stuck(int q) {
|
|
147 return start(q) == end(q);
|
|
148 }
|
|
149
|
|
150 private int depth(int q) {
|
|
151 return q % 50;
|
|
152 }
|
|
153
|
|
154 private int start(int q) {
|
|
155 return q / (50*50);
|
|
156 }
|
|
157
|
|
158 private int end(int q) {
|
|
159 return (q / 50) % 50;
|
|
160 }
|
|
161
|
|
162 private int calcWalkingRange(int depth, int pos) {
|
|
163 return calcWalkingRange(depth, pos, Integer.MAX_VALUE, Integer.MIN_VALUE);
|
|
164 }
|
|
165
|
|
166 private int calcWalkingRange(int depth, int pos, int airOverrideStart, int airOverrideEnd) {
|
|
167 int left = pos, right = pos;
|
|
168 if (depth >= r) return (c-1)*50 + depth;
|
|
169
|
|
170 while (left > 0) {
|
|
171 if (grid[depth][left-1] == ROCK && (left-1 < airOverrideStart || left-1 > airOverrideEnd)) break;
|
|
172 if (depth < r-1 && grid[depth+1][left-1] == AIR) {
|
|
173 left--;
|
|
174 break;
|
|
175 }
|
|
176 left--;
|
|
177 }
|
|
178 while (right < c-1) {
|
|
179 if (grid[depth][right+1] == ROCK && (right+1 < airOverrideStart || right+1 > airOverrideEnd)) break;
|
|
180 if (depth < r-1 && grid[depth+1][right+1] == AIR) {
|
|
181 right++;
|
|
182 break;
|
|
183 }
|
|
184 right++;
|
|
185 }
|
|
186
|
|
187 return left *50*50 + right*50 + depth;
|
|
188 }
|
|
189 }
|