test/compiler/5091921/Test6890943.java

changeset 2877
bad7ecd0b6ed
child 4618
514efad5e81a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/compiler/5091921/Test6890943.java	Wed May 04 13:12:42 2011 -0700
     1.3 @@ -0,0 +1,189 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +/**
    1.29 + * @test
    1.30 + * @bug 6890943
    1.31 + * @summary JVM mysteriously gives wrong result on 64-bit 1.6 VMs in hotspot mode.
    1.32 + *
    1.33 + * @run shell Test6890943.sh
    1.34 + */
    1.35 +import java.util.*;
    1.36 +import java.io.*;
    1.37 +import java.util.regex.*;
    1.38 +
    1.39 +public class Test6890943 {
    1.40 +  public static final boolean AIR = true, ROCK = false;
    1.41 +  public static void main(String[] args) {
    1.42 +    new Test6890943().go();
    1.43 +  }
    1.44 +
    1.45 +  int r, c, f, t;
    1.46 +  boolean[][] grid;
    1.47 +
    1.48 +  public void go() {
    1.49 +    Scanner s = new Scanner(System.in);
    1.50 +    s.useDelimiter("\\s+");
    1.51 +    int T = s.nextInt();
    1.52 +    for (t = 0 ; t < T ; t++) {
    1.53 +      r = s.nextInt(); c = s.nextInt(); f = s.nextInt();
    1.54 +      grid = new boolean[r][c];
    1.55 +      for (int x = 0 ; x < r ; x++) {
    1.56 +        String line = s.next();
    1.57 +        for (int y = 0 ; y < c ; y++) grid[x][y] = line.charAt(y) == '.';
    1.58 +      }
    1.59 +      int digs = solve();
    1.60 +      String res = digs == -1 ? "No" : "Yes " + digs;
    1.61 +      System.out.printf("Case #%d: %s\n", t+1, res);
    1.62 +    }
    1.63 +  }
    1.64 +
    1.65 +  Map<Integer, Integer> M = new HashMap<Integer, Integer>();
    1.66 +
    1.67 +  private int solve() {
    1.68 +    M = new HashMap<Integer, Integer>();
    1.69 +    M.put(calcWalkingRange(0, 0), 0);
    1.70 +    for (int digDown = 0 ; digDown < r ; digDown++) {
    1.71 +      Map<Integer, Integer> tries = new HashMap<Integer, Integer>();
    1.72 +      for (Map.Entry<Integer, Integer> m : M.entrySet()) {
    1.73 +        int q = m.getKey();
    1.74 +        if (depth(q) != (digDown)) continue;
    1.75 +        if (stuck(q)) continue;
    1.76 +        tries.put(q, m.getValue());
    1.77 +      }
    1.78 +
    1.79 +      for (Map.Entry<Integer, Integer> m : tries.entrySet()) {
    1.80 +        int q = m.getKey();
    1.81 +        int fallLeftDelta = 0, fallRightDelta = 0;
    1.82 +        //fall left
    1.83 +        int fallLeft = fall(digDown, start(q));
    1.84 +        if (fallLeft > 0) {
    1.85 +          fallLeftDelta = 1;
    1.86 +          if (fallLeft <= f) addToM(calcWalkingRange(digDown+fallLeft, start(q)), m.getValue());
    1.87 +        }
    1.88 +
    1.89 +        //fall right
    1.90 +        int fallRight = fall(digDown, end(q));
    1.91 +        if (fallRight > 0) {
    1.92 +          fallRightDelta = 1;
    1.93 +
    1.94 +          if (fallRight <= f) addToM(calcWalkingRange(digDown+fallRight, end(q)), m.getValue());
    1.95 +        }
    1.96 +
    1.97 +        for (int p = start(q) + fallLeftDelta ; p <= end(q) - fallRightDelta ; p++) {
    1.98 +          //goLeft
    1.99 +          for (int digSpot = p ; digSpot > start(q) +fallLeftDelta ; digSpot--) {
   1.100 +            int fallDown = 1+fall(digDown+1, digSpot);
   1.101 +            if (fallDown <= f) {
   1.102 +              if (fallDown == 1) {
   1.103 +                addToM(calcWalkingRange(digDown + 1, digSpot, digSpot, p), m.getValue() + Math.abs(digSpot-p)+1);
   1.104 +              } else {
   1.105 +                addToM(calcWalkingRange(digDown + fallDown, digSpot), m.getValue() + Math.abs(digSpot-p)+1);
   1.106 +              }
   1.107 +            }
   1.108 +          }
   1.109 +
   1.110 +          //goRight
   1.111 +          for (int digSpot = p ; digSpot < end(q)-fallRightDelta ;digSpot++) {
   1.112 +            int fallDown = 1+fall(digDown+1, digSpot);
   1.113 +            if (fallDown <= f) {
   1.114 +              if (fallDown == 1) {
   1.115 +                addToM(calcWalkingRange(digDown + 1, digSpot, p, digSpot), m.getValue() + Math.abs(digSpot-p)+1);
   1.116 +              } else {
   1.117 +                addToM(calcWalkingRange(digDown + fallDown, digSpot), m.getValue() + Math.abs(digSpot-p)+1);
   1.118 +              }
   1.119 +            }
   1.120 +          }
   1.121 +        }
   1.122 +      }
   1.123 +    }
   1.124 +
   1.125 +    int result = Integer.MAX_VALUE;
   1.126 +    for (Map.Entry<Integer, Integer> m : M.entrySet()) {
   1.127 +      if (depth(m.getKey()) == r-1) result = Math.min(m.getValue(), result);
   1.128 +    }
   1.129 +
   1.130 +    if (result == Integer.MAX_VALUE) return -1;
   1.131 +    return result;
   1.132 +  }
   1.133 +
   1.134 +  private void addToM(int q, int i) {
   1.135 +    Integer original = M.get(q);
   1.136 +    if ( original == null ) M.put(q, i);
   1.137 +    else M.put(q, Math.min(original, i));
   1.138 +  }
   1.139 +
   1.140 +  private int fall(int row, int column) {
   1.141 +    int res = 0;
   1.142 +    for ( int p = row+1 ; p < r ; p++) {
   1.143 +      if (grid[p][column] == AIR) res++;
   1.144 +      else break;
   1.145 +    }
   1.146 +    return res;
   1.147 +  }
   1.148 +
   1.149 +  private boolean stuck(int q) {
   1.150 +    return start(q) == end(q);
   1.151 +  }
   1.152 +
   1.153 +  private int depth(int q) {
   1.154 +    return q % 50;
   1.155 +  }
   1.156 +
   1.157 +  private int start(int q) {
   1.158 +    return q / (50*50);
   1.159 +  }
   1.160 +
   1.161 +  private int end(int q) {
   1.162 +    return (q / 50) % 50;
   1.163 +  }
   1.164 +
   1.165 +  private int calcWalkingRange(int depth, int pos) {
   1.166 +    return calcWalkingRange(depth, pos, Integer.MAX_VALUE, Integer.MIN_VALUE);
   1.167 +  }
   1.168 +
   1.169 +  private int calcWalkingRange(int depth, int pos, int airOverrideStart, int airOverrideEnd) {
   1.170 +    int left = pos, right = pos;
   1.171 +    if (depth >= r) return (c-1)*50 + depth;
   1.172 +
   1.173 +    while (left > 0) {
   1.174 +      if (grid[depth][left-1] == ROCK && (left-1 < airOverrideStart || left-1 > airOverrideEnd)) break;
   1.175 +      if (depth < r-1 && grid[depth+1][left-1] == AIR) {
   1.176 +        left--;
   1.177 +        break;
   1.178 +      }
   1.179 +      left--;
   1.180 +    }
   1.181 +    while (right < c-1) {
   1.182 +      if (grid[depth][right+1] == ROCK && (right+1 < airOverrideStart || right+1 > airOverrideEnd)) break;
   1.183 +      if (depth < r-1 && grid[depth+1][right+1] == AIR) {
   1.184 +        right++;
   1.185 +        break;
   1.186 +      }
   1.187 +      right++;
   1.188 +    }
   1.189 +
   1.190 +    return left *50*50 + right*50 + depth;
   1.191 +  }
   1.192 +}

mercurial