結果

問題 No.611 Day of the Mountain
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-20 18:29:16
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 371 ms / 2,017 ms
コード長 3,387 bytes
コンパイル時間 2,271 ms
コンパイル使用メモリ 108,252 KB
実行使用メモリ 5,668 KB
最終ジャッジ日時 2023-09-04 00:44:16
合計ジャッジ時間 3,772 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 364 ms
5,620 KB
testcase_01 AC 364 ms
5,656 KB
testcase_02 AC 363 ms
5,664 KB
testcase_03 AC 371 ms
5,668 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum MO = 201_712_111L;
enum INF = 10^^9;

int H, W;
string[] A;

void main() {
  try {
    for (; ; ) {
      H = readInt();
      W = readInt();
      A = new string[H];
      foreach (x; 0 .. H) {
        A[x] = readToken();
      }
      
      if (H < W) {
        auto AT = A;
        swap(H, W);
        A = new string[H];
        foreach (x; 0 .. H) {
          foreach (y; 0 .. W) {
            A[x] ~= AT[y][x];
          }
        }
      }
      debug {
        writefln("H = %s, W = %s, A = %s", H, W, A);
      }
      assert(W <= 17);
      
      auto dist = new int[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        dist[x][y] = INF;
        if (x == 0 && y == 0) {
          dist[x][y] = 0;
        }
        if (x > 0) {
          chmin(dist[x][y], dist[x - 1][y]);
        }
        if (y > 0) {
          chmin(dist[x][y], dist[x][y - 1]);
        }
        dist[x][y] += (A[x][y] == '?') ? 1 : (A[x][y] - '0');
      }
      debug {
        writeln("  dist = ", dist);
      }
      
      auto dp = new long[1 << W];
      dp[1 << 0] = 1;
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        if (x == 0 && y == 0) {
          continue;
        }
        auto dpNext = new long[1 << W];
        foreach (p; 0 .. 1 << W) {
          int c = INF;
          if (x > 0 && ((p >> y) & 1)) {
            chmin(c, dist[x - 1][y]);
          }
          if (y > 0 && ((p >> (y - 1)) & 1)) {
            chmin(c, dist[x][y - 1]);
          }
          if (A[x][y] == '?') {
            if (dist[x][y] == c + 1) {
              dpNext[p & ~(1 << y)] += dp[p] * 8;
              dpNext[p |  (1 << y)] += dp[p];
            } else {
              dpNext[p & ~(1 << y)] += dp[p] * 9;
            }
          } else {
            if (dist[x][y] == c + (A[x][y] - '0')) {
              dpNext[p |  (1 << y)] += dp[p];
            } else {
              dpNext[p & ~(1 << y)] += dp[p];
            }
          }
        }
        dpNext[] %= MO;
        dp = dpNext;
      }
      
      long ans;
      foreach (p; 0 .. 1 << W) {
        if ((p >> (W - 1)) & 1) {
          ans += dp[p];
        }
      }
      ans %= MO;
      writeln(dist[H - 1][W - 1]);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0