結果

問題 No.124 門松列(3)
ユーザー nebukuro09nebukuro09
提出日時 2016-11-03 21:32:02
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,461 bytes
コンパイル時間 858 ms
コンパイル使用メモリ 101,456 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-09-02 22:42:50
合計ジャッジ時間 2,197 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 3 ms
4,372 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 2 ms
4,368 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 1 ms
4,368 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 1 ms
4,368 KB
testcase_14 AC 2 ms
4,372 KB
testcase_15 AC 1 ms
4,372 KB
testcase_16 AC 2 ms
4,368 KB
testcase_17 AC 1 ms
4,368 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 1 ms
4,368 KB
testcase_21 AC 2 ms
4,368 KB
testcase_22 AC 1 ms
4,368 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 3 ms
4,368 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,372 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 2 ms
4,368 KB
testcase_29 AC 3 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;

bool isKadomatsu(int a, int b, int c) {
  return ((a < b && b > c && a != c) || (a > b && b < c && a != c));
}

void main() {
  auto input = readln.split.map!(to!int);
  int W = input[0];
  int H = input[1];
  int[][] B = new int[][](H, W);
  foreach (i; iota(H))
    B[i] = readln.split.map!(to!int).array;

  bool[int] visited;
  DList!(Tuple!(int, int, int, int)) queue;
  queue.insert(tuple(1, 0, B[0][0], 1));
  queue.insert(tuple(0, 1, B[0][0], 1));
  int row, col, prev, cost, nrow, ncol;
  int[4] dx = [0, 0, -1, 1];
  int[4] dy = [-1, 1, 0, 0];

  while (!queue.empty()) {
    auto node = queue.front;
    queue.removeFront();
    row = node[0];
    col = node[1];
    prev = node[2];
    cost = node[3];
    if (row == H-1 && col == W-1) { writeln(cost); return; }
    if (row*10000+col*100+prev in visited)
      continue;
    visited[row*10000+col*100+prev] = true;

    foreach (d; iota(4)) {
      nrow = row + dx[d];
      ncol = col + dy[d];
      if (nrow < 0 || nrow >= H || ncol < 0 || ncol >= W)
        continue;
      if (nrow*10000+ncol*100+B[row][col] in visited)
        continue;
      if (!isKadomatsu(prev, B[row][col], B[nrow][ncol]))
        continue;
      queue.insert(tuple(nrow, ncol, B[row][col], cost+1));
    }
  }
  writeln(-1);
}
0