結果

問題 No.124 門松列(3)
ユーザー te-shte-sh
提出日時 2016-09-07 12:08:41
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,866 bytes
コンパイル時間 2,912 ms
コンパイル使用メモリ 118,944 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-09-02 21:58:34
合計ジャッジ時間 2,076 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

struct point {
  int x, y;

  point opBinary(string op)(point rhs) {
    static if (op == "+") return point(x + rhs.x, y + rhs.y);
    else static if (op == "-") return point(x - rhs.x, y - rhs.y);
  }
}

const auto sibPoints = [point(-1, 0), point(0, -1), point(1, 0), point(0, 1)];

alias Tuple!(point, "curr", point, "prev", size_t, "len") qItem;

void main()
{
  auto rd = readln.split.map!(to!size_t);
  auto w = rd[0], h = rd[1];
  auto mij = iota(h).map!(_ => readln.split.map!(to!int).array).array;

  bool valid(point p) {
    return p.x >= 0 && p.x < w && p.y >= 0 && p.y < h;
  }

  bool isKadomatsu(point p1, point p2, point p3) {
    auto m1 = mij[p1.y][p1.x], m2 = mij[p2.y][p2.x], m3 = mij[p3.y][p3.x];
    return m1 != m2 && m2 != m3 && m1 != m3 && (m1 > m2 && m3 > m2 || m1 < m2 && m3 < m2);
  }

  int calc() {
    auto visited = new point[][][](h, w);
    auto queue = new DList!qItem(qItem(point(0, 1), point(0, 0), 1),
                                 qItem(point(1, 0), point(0, 0), 1));
    auto goal = point(w.to!int - 1, h.to!int - 1);
    visited[0][0] = [point(1, 0), point(0, 1)];
    visited[1][0] = [point(0, 0)];
    visited[0][1] = [point(0, 0)];

    while (!queue.empty) {
      auto qi = queue.front;
      queue.removeFront;

      foreach (sib; sibPoints) {
        auto np = qi.curr + sib;
        if (!valid(np) || visited[np.y][np.x].canFind(qi.curr) || !isKadomatsu(np, qi.curr, qi.prev))
          continue;
        if (np == goal)
          return qi.len.to!int + 1;
        queue.insertBack(qItem(np, qi.curr, qi.len + 1));
        visited[np.y][np.x] ~= qi.curr;
      }
    }
    return -1;
  }

  writeln(calc);
}
0