You need to sign in or sign up before continuing.
Test6890943.java 5.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

/**
 * @test
 * @bug 6890943
 * @summary JVM mysteriously gives wrong result on 64-bit 1.6 VMs in hotspot mode.
 *
30
 * @run shell/timeout=240 Test6890943.sh
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
 */
import java.util.*;
import java.io.*;
import java.util.regex.*;

public class Test6890943 {
  public static final boolean AIR = true, ROCK = false;
  public static void main(String[] args) {
    new Test6890943().go();
  }

  int r, c, f, t;
  boolean[][] grid;

  public void go() {
    Scanner s = new Scanner(System.in);
    s.useDelimiter("\\s+");
    int T = s.nextInt();
    for (t = 0 ; t < T ; t++) {
      r = s.nextInt(); c = s.nextInt(); f = s.nextInt();
      grid = new boolean[r][c];
      for (int x = 0 ; x < r ; x++) {
        String line = s.next();
        for (int y = 0 ; y < c ; y++) grid[x][y] = line.charAt(y) == '.';
      }
      int digs = solve();
      String res = digs == -1 ? "No" : "Yes " + digs;
      System.out.printf("Case #%d: %s\n", t+1, res);
    }
  }

  Map<Integer, Integer> M = new HashMap<Integer, Integer>();

  private int solve() {
    M = new HashMap<Integer, Integer>();
    M.put(calcWalkingRange(0, 0), 0);
    for (int digDown = 0 ; digDown < r ; digDown++) {
      Map<Integer, Integer> tries = new HashMap<Integer, Integer>();
      for (Map.Entry<Integer, Integer> m : M.entrySet()) {
        int q = m.getKey();
        if (depth(q) != (digDown)) continue;
        if (stuck(q)) continue;
        tries.put(q, m.getValue());
      }

      for (Map.Entry<Integer, Integer> m : tries.entrySet()) {
        int q = m.getKey();
        int fallLeftDelta = 0, fallRightDelta = 0;
        //fall left
        int fallLeft = fall(digDown, start(q));
        if (fallLeft > 0) {
          fallLeftDelta = 1;
          if (fallLeft <= f) addToM(calcWalkingRange(digDown+fallLeft, start(q)), m.getValue());
        }

        //fall right
        int fallRight = fall(digDown, end(q));
        if (fallRight > 0) {
          fallRightDelta = 1;

          if (fallRight <= f) addToM(calcWalkingRange(digDown+fallRight, end(q)), m.getValue());
        }

        for (int p = start(q) + fallLeftDelta ; p <= end(q) - fallRightDelta ; p++) {
          //goLeft
          for (int digSpot = p ; digSpot > start(q) +fallLeftDelta ; digSpot--) {
            int fallDown = 1+fall(digDown+1, digSpot);
            if (fallDown <= f) {
              if (fallDown == 1) {
                addToM(calcWalkingRange(digDown + 1, digSpot, digSpot, p), m.getValue() + Math.abs(digSpot-p)+1);
              } else {
                addToM(calcWalkingRange(digDown + fallDown, digSpot), m.getValue() + Math.abs(digSpot-p)+1);
              }
            }
          }

          //goRight
          for (int digSpot = p ; digSpot < end(q)-fallRightDelta ;digSpot++) {
            int fallDown = 1+fall(digDown+1, digSpot);
            if (fallDown <= f) {
              if (fallDown == 1) {
                addToM(calcWalkingRange(digDown + 1, digSpot, p, digSpot), m.getValue() + Math.abs(digSpot-p)+1);
              } else {
                addToM(calcWalkingRange(digDown + fallDown, digSpot), m.getValue() + Math.abs(digSpot-p)+1);
              }
            }
          }
        }
      }
    }

    int result = Integer.MAX_VALUE;
    for (Map.Entry<Integer, Integer> m : M.entrySet()) {
      if (depth(m.getKey()) == r-1) result = Math.min(m.getValue(), result);
    }

    if (result == Integer.MAX_VALUE) return -1;
    return result;
  }

  private void addToM(int q, int i) {
    Integer original = M.get(q);
    if ( original == null ) M.put(q, i);
    else M.put(q, Math.min(original, i));
  }

  private int fall(int row, int column) {
    int res = 0;
    for ( int p = row+1 ; p < r ; p++) {
      if (grid[p][column] == AIR) res++;
      else break;
    }
    return res;
  }

  private boolean stuck(int q) {
    return start(q) == end(q);
  }

  private int depth(int q) {
    return q % 50;
  }

  private int start(int q) {
    return q / (50*50);
  }

  private int end(int q) {
    return (q / 50) % 50;
  }

  private int calcWalkingRange(int depth, int pos) {
    return calcWalkingRange(depth, pos, Integer.MAX_VALUE, Integer.MIN_VALUE);
  }

  private int calcWalkingRange(int depth, int pos, int airOverrideStart, int airOverrideEnd) {
    int left = pos, right = pos;
    if (depth >= r) return (c-1)*50 + depth;

    while (left > 0) {
      if (grid[depth][left-1] == ROCK && (left-1 < airOverrideStart || left-1 > airOverrideEnd)) break;
      if (depth < r-1 && grid[depth+1][left-1] == AIR) {
        left--;
        break;
      }
      left--;
    }
    while (right < c-1) {
      if (grid[depth][right+1] == ROCK && (right+1 < airOverrideStart || right+1 > airOverrideEnd)) break;
      if (depth < r-1 && grid[depth+1][right+1] == AIR) {
        right++;
        break;
      }
      right++;
    }

    return left *50*50 + right*50 + depth;
  }
}