結果

問題 No.2412 YOU Grow Bigger!
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-08-11 22:11:05
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 10 ms / 6,000 ms
コード長 3,848 bytes
コンパイル時間 3,874 ms
コンパイル使用メモリ 105,284 KB
実行使用メモリ 13,372 KB
最終ジャッジ日時 2023-08-13 13:21:16
合計ジャッジ時間 5,402 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 10 ms
12,884 KB
testcase_04 AC 10 ms
12,356 KB
testcase_05 AC 10 ms
12,392 KB
testcase_06 AC 10 ms
12,320 KB
testcase_07 AC 10 ms
13,372 KB
testcase_08 AC 10 ms
12,400 KB
testcase_09 AC 10 ms
12,652 KB
testcase_10 AC 8 ms
10,404 KB
testcase_11 AC 4 ms
5,420 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 8 ms
9,820 KB
testcase_14 AC 7 ms
9,288 KB
testcase_15 AC 9 ms
11,264 KB
testcase_16 AC 9 ms
12,856 KB
testcase_17 AC 8 ms
11,040 KB
testcase_18 AC 7 ms
9,304 KB
testcase_19 AC 6 ms
7,468 KB
testcase_20 AC 5 ms
6,164 KB
testcase_21 AC 5 ms
7,076 KB
testcase_22 AC 7 ms
11,256 KB
testcase_23 AC 7 ms
7,764 KB
testcase_24 AC 8 ms
11,004 KB
testcase_25 AC 6 ms
7,504 KB
testcase_26 AC 7 ms
9,352 KB
testcase_27 AC 5 ms
7,096 KB
testcase_28 AC 8 ms
10,344 KB
testcase_29 AC 9 ms
11,620 KB
testcase_30 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, 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; }

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 INF = 10^^9;

void main() {
  try {
    for (; ; ) {
      const M = readInt;
      const N = readInt;
      auto S = new string[M];
      foreach (x; 0 .. M) {
        S[x] = readToken;
      }
      
      auto a = new char[][](M, N);
      foreach (x; 0 .. M) {
        a[x][] = '.';
      }
      foreach (x; 0 .. M) foreach (y; 0 .. N) if (S[x][y] == '#') {
        foreach (dx; -1 .. +1 + 1) foreach (dy; -1 .. +1 + 1) {
          const xx = x + dx;
          const yy = y + dy;
          if (0 <= xx && xx < M && 0 <= yy && yy < N) {
            a[xx][yy] = '#';
          }
        }
      }
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        if (x == 0 || x == M - 1 || y == 0 || y == N - 1) {
          a[x][y] = '#';
        }
        if ((x < 2 && y < 2) || (x >= M - 2 && y >= N - 2)) {
          a[x][y] = '.';
        }
      }
      debug {
        foreach (x; 0 .. M) {
          writeln(a[x]);
        }
      }
      
      auto transUp = new int[1 << 9];
      auto transRight = new int[1 << 9];
      foreach (p; 0 .. 1 << 9) {
        transUp[p] = p >> 3;
        int pp = p;
        foreach (dx; 0 .. 3) pp &= ~(1 << (dx * 3));
        transRight[p] = pp >> 1;
      }
      
      auto dp = new int[][][](M, N, 1 << 9);
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        dp[x][y][] = INF;
      }
      dp[M - 1][0][0] = 0;
      foreach_reverse (x; 0 .. M) foreach (y; 0 .. N) {
        int has;
        foreach (dx; 0 .. 3) foreach (dy; 0 .. 3) {
          const xx = x - dx;
          const yy = y + dy;
          if (xx >= 0 && yy < N && a[xx][yy] == '#') {
            has |= 1 << (dx * 3 + dy);
          }
        }
        bool canPut = true;
        canPut = canPut && (1 <= x - 1 && x - 1 < M - 1 && 1 <= y + 1 && y + 1 < N - 1);
        canPut = canPut && !(x - 1 < 3 && y + 1 < 3);
        canPut = canPut && !(x - 1 >= M - 3 && y + 1 >= N - 3);
        debug {
          writeln(x, " ", y, " ", canPut, " ", dp[x][y].minElement);
        }
        foreach (p; 0 .. 1 << 9) if (dp[x][y][p] < INF) {
          const pp = has | p;
          if (x - 1 >= 0) {
            if (pp >> 3 & 1) {
              chmin(dp[x - 1][y][transUp[pp]], dp[x][y][p]);
            }
            if (canPut) {
              chmin(dp[x - 1][y][transUp[$ - 1]], dp[x][y][p] + 1);
            }
          }
          if (y + 1 < N) {
            if (pp >> 1 & 1) {
              chmin(dp[x][y + 1][transRight[pp]], dp[x][y][p]);
            }
            if (canPut) {
              chmin(dp[x][y + 1][transRight[$ - 1]], dp[x][y][p] + 1);
            }
          }
        }
      }
      
      const ans = dp[0][N - 1].minElement;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0