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