1 /* 2 * Copyright (c) 2011, 2014, 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 main/othervm/timeout=240 Test6890943 31 */ 32 33 import java.io.*; 34 import java.nio.file.Path; 35 import java.nio.file.Paths; 36 import java.util.HashMap; 37 import java.util.Map; 38 import java.util.Scanner; 39 40 public class Test6890943 { 41 public static final boolean AIR = true, ROCK = false; 42 private static final Path PATH = Paths.get(System.getProperty("test.src", ".")); 43 private static final Path INPUT_FILE = PATH.resolve("input6890943.txt"); 44 private static final Path GOLDEN_FILE = PATH.resolve("output6890943.txt"); 45 46 public static void main(String[] args) { 47 new Test6890943().go(); 48 } 49 50 int r, c, f, t; 51 boolean[][] grid; 52 53 public void go() { 54 Scanner in, golden; 55 try { 56 in = new Scanner(new FileInputStream(INPUT_FILE.toFile())); 57 golden = new Scanner(new FileInputStream(GOLDEN_FILE.toFile())); 58 } catch (FileNotFoundException e) { 59 throw new RuntimeException("TEST failure: can't open test file", e); 60 } 61 in.useDelimiter("\\s+"); 62 golden.useDelimiter("\\s+"); 63 64 int T = in.nextInt(); 65 for (t = 0; t < T; t++) { 66 r = in.nextInt(); 67 c = in.nextInt(); 68 f = in.nextInt(); 69 grid = new boolean[r][c]; 70 for (int x = 0; x < r; x++) { 71 String line = in.next(); 72 for (int y = 0; y < c; y++) { 73 grid[x][y] = line.charAt(y) == '.'; 74 } 75 } 76 int digs = solve(); 77 String result = "Case #" + (t + 1) + ": " + (digs == -1 ? "No" : "Yes " + digs); 78 System.out.println(result); 79 // Compare with golden string from the file 80 String goldenStr = golden.nextLine(); 81 if (!result.equals(goldenStr)) { 82 System.err.println("FAIL: strings are not equal\n" 83 + "-- Result: " + result + "\n" 84 + "-- Golden: " + goldenStr); 85 throw new RuntimeException("FAIL: Result string is not equal to the golden"); 86 } 87 } 88 } 89 90 Map<Integer, Integer> M = new HashMap<Integer, Integer>(); 91 92 private int solve() { 93 M = new HashMap<Integer, Integer>(); 94 M.put(calcWalkingRange(0, 0), 0); 95 for (int digDown = 0; digDown < r; digDown++) { 96 Map<Integer, Integer> tries = new HashMap<Integer, Integer>(); 97 for (Map.Entry<Integer, Integer> m : M.entrySet()) { 98 int q = m.getKey(); 99 if (depth(q) != (digDown)) continue; 100 if (stuck(q)) continue; 101 tries.put(q, m.getValue()); 102 } 103 104 for (Map.Entry<Integer, Integer> m : tries.entrySet()) { 105 int q = m.getKey(); 106 int fallLeftDelta = 0, fallRightDelta = 0; 107 //fall left 108 int fallLeft = fall(digDown, start(q)); 109 if (fallLeft > 0) { 110 fallLeftDelta = 1; 111 if (fallLeft <= f) addToM(calcWalkingRange(digDown + fallLeft, start(q)), m.getValue()); 112 } 113 114 //fall right 115 int fallRight = fall(digDown, end(q)); 116 if (fallRight > 0) { 117 fallRightDelta = 1; 118 119 if (fallRight <= f) addToM(calcWalkingRange(digDown + fallRight, end(q)), m.getValue()); 120 } 121 122 for (int p = start(q) + fallLeftDelta; p <= end(q) - fallRightDelta; p++) { 123 //goLeft 124 for (int digSpot = p; digSpot > start(q) + fallLeftDelta; digSpot--) { 125 int fallDown = 1 + fall(digDown + 1, digSpot); 126 if (fallDown <= f) { 127 if (fallDown == 1) { 128 addToM(calcWalkingRange(digDown + 1, digSpot, digSpot, p), 129 m.getValue() + Math.abs(digSpot - p) + 1); 130 } else { 131 addToM(calcWalkingRange(digDown + fallDown, digSpot), 132 m.getValue() + Math.abs(digSpot - p) + 1); 133 } 134 } 135 } 136 137 //goRight 138 for (int digSpot = p; digSpot < end(q) - fallRightDelta; digSpot++) { 139 int fallDown = 1 + fall(digDown + 1, digSpot); 140 if (fallDown <= f) { 141 if (fallDown == 1) { 142 addToM(calcWalkingRange(digDown + 1, digSpot, p, digSpot), 143 m.getValue() + Math.abs(digSpot - p) + 1); 144 } else { 145 addToM(calcWalkingRange(digDown + fallDown, digSpot), 146 m.getValue() + Math.abs(digSpot - p) + 1); 147 } 148 } 149 } 150 } 151 } 152 } 153 154 int result = Integer.MAX_VALUE; 155 for (Map.Entry<Integer, Integer> m : M.entrySet()) { 156 if (depth(m.getKey()) == r - 1) result = Math.min(m.getValue(), result); 157 } 158 159 if (result == Integer.MAX_VALUE) return -1; 160 return result; 161 } 162 163 private void addToM(int q, int i) { 164 Integer original = M.get(q); 165 if (original == null) M.put(q, i); 166 else M.put(q, Math.min(original, i)); 167 } 168 169 private int fall(int row, int column) { 170 int res = 0; 171 for (int p = row + 1; p < r; p++) { 172 if (grid[p][column] == AIR) res++; 173 else break; 174 } 175 return res; 176 } 177 178 private boolean stuck(int q) { 179 return start(q) == end(q); 180 } 181 182 private int depth(int q) { 183 return q % 50; 184 } 185 186 private int start(int q) { 187 return q / (50 * 50); 188 } 189 190 private int end(int q) { 191 return (q / 50) % 50; 192 } 193 194 private int calcWalkingRange(int depth, int pos) { 195 return calcWalkingRange(depth, pos, Integer.MAX_VALUE, Integer.MIN_VALUE); 196 } 197 198 private int calcWalkingRange(int depth, int pos, int airOverrideStart, int airOverrideEnd) { 199 int left = pos, right = pos; 200 if (depth >= r) return (c - 1) * 50 + depth; 201 202 while (left > 0) { 203 if (grid[depth][left - 1] == ROCK && (left - 1 < airOverrideStart || left - 1 > airOverrideEnd)) break; 204 if (depth < r - 1 && grid[depth + 1][left - 1] == AIR) { 205 left--; 206 break; 207 } 208 left--; 209 } 210 while (right < c - 1) { 211 if (grid[depth][right + 1] == ROCK && (right + 1 < airOverrideStart || right + 1 > airOverrideEnd)) break; 212 if (depth < r - 1 && grid[depth + 1][right + 1] == AIR) { 213 right++; 214 break; 215 } 216 right++; 217 } 218 219 return left * 50 * 50 + right * 50 + depth; 220 } 221 }