Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/compiler/5091921/Test6890943.java
32285 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/**25* @test26* @bug 689094327* @summary JVM mysteriously gives wrong result on 64-bit 1.6 VMs in hotspot mode.28*29* @run shell/timeout=240 Test6890943.sh30*/31import java.util.*;32import java.io.*;33import java.util.regex.*;3435public class Test6890943 {36public static final boolean AIR = true, ROCK = false;37public static void main(String[] args) {38new Test6890943().go();39}4041int r, c, f, t;42boolean[][] grid;4344public void go() {45Scanner s = new Scanner(System.in);46s.useDelimiter("\\s+");47int T = s.nextInt();48for (t = 0 ; t < T ; t++) {49r = s.nextInt(); c = s.nextInt(); f = s.nextInt();50grid = new boolean[r][c];51for (int x = 0 ; x < r ; x++) {52String line = s.next();53for (int y = 0 ; y < c ; y++) grid[x][y] = line.charAt(y) == '.';54}55int digs = solve();56String res = digs == -1 ? "No" : "Yes " + digs;57System.out.printf("Case #%d: %s\n", t+1, res);58}59}6061Map<Integer, Integer> M = new HashMap<Integer, Integer>();6263private int solve() {64M = new HashMap<Integer, Integer>();65M.put(calcWalkingRange(0, 0), 0);66for (int digDown = 0 ; digDown < r ; digDown++) {67Map<Integer, Integer> tries = new HashMap<Integer, Integer>();68for (Map.Entry<Integer, Integer> m : M.entrySet()) {69int q = m.getKey();70if (depth(q) != (digDown)) continue;71if (stuck(q)) continue;72tries.put(q, m.getValue());73}7475for (Map.Entry<Integer, Integer> m : tries.entrySet()) {76int q = m.getKey();77int fallLeftDelta = 0, fallRightDelta = 0;78//fall left79int fallLeft = fall(digDown, start(q));80if (fallLeft > 0) {81fallLeftDelta = 1;82if (fallLeft <= f) addToM(calcWalkingRange(digDown+fallLeft, start(q)), m.getValue());83}8485//fall right86int fallRight = fall(digDown, end(q));87if (fallRight > 0) {88fallRightDelta = 1;8990if (fallRight <= f) addToM(calcWalkingRange(digDown+fallRight, end(q)), m.getValue());91}9293for (int p = start(q) + fallLeftDelta ; p <= end(q) - fallRightDelta ; p++) {94//goLeft95for (int digSpot = p ; digSpot > start(q) +fallLeftDelta ; digSpot--) {96int fallDown = 1+fall(digDown+1, digSpot);97if (fallDown <= f) {98if (fallDown == 1) {99addToM(calcWalkingRange(digDown + 1, digSpot, digSpot, p), m.getValue() + Math.abs(digSpot-p)+1);100} else {101addToM(calcWalkingRange(digDown + fallDown, digSpot), m.getValue() + Math.abs(digSpot-p)+1);102}103}104}105106//goRight107for (int digSpot = p ; digSpot < end(q)-fallRightDelta ;digSpot++) {108int fallDown = 1+fall(digDown+1, digSpot);109if (fallDown <= f) {110if (fallDown == 1) {111addToM(calcWalkingRange(digDown + 1, digSpot, p, digSpot), m.getValue() + Math.abs(digSpot-p)+1);112} else {113addToM(calcWalkingRange(digDown + fallDown, digSpot), m.getValue() + Math.abs(digSpot-p)+1);114}115}116}117}118}119}120121int result = Integer.MAX_VALUE;122for (Map.Entry<Integer, Integer> m : M.entrySet()) {123if (depth(m.getKey()) == r-1) result = Math.min(m.getValue(), result);124}125126if (result == Integer.MAX_VALUE) return -1;127return result;128}129130private void addToM(int q, int i) {131Integer original = M.get(q);132if ( original == null ) M.put(q, i);133else M.put(q, Math.min(original, i));134}135136private int fall(int row, int column) {137int res = 0;138for ( int p = row+1 ; p < r ; p++) {139if (grid[p][column] == AIR) res++;140else break;141}142return res;143}144145private boolean stuck(int q) {146return start(q) == end(q);147}148149private int depth(int q) {150return q % 50;151}152153private int start(int q) {154return q / (50*50);155}156157private int end(int q) {158return (q / 50) % 50;159}160161private int calcWalkingRange(int depth, int pos) {162return calcWalkingRange(depth, pos, Integer.MAX_VALUE, Integer.MIN_VALUE);163}164165private int calcWalkingRange(int depth, int pos, int airOverrideStart, int airOverrideEnd) {166int left = pos, right = pos;167if (depth >= r) return (c-1)*50 + depth;168169while (left > 0) {170if (grid[depth][left-1] == ROCK && (left-1 < airOverrideStart || left-1 > airOverrideEnd)) break;171if (depth < r-1 && grid[depth+1][left-1] == AIR) {172left--;173break;174}175left--;176}177while (right < c-1) {178if (grid[depth][right+1] == ROCK && (right+1 < airOverrideStart || right+1 > airOverrideEnd)) break;179if (depth < r-1 && grid[depth+1][right+1] == AIR) {180right++;181break;182}183right++;184}185186return left *50*50 + right*50 + depth;187}188}189190191