結果

問題 No.13 囲みたい!
ユーザー te-shte-sh
提出日時 2017-01-13 19:05:21
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 188 ms / 5,000 ms
コード長 1,779 bytes
コンパイル時間 725 ms
コンパイル使用メモリ 96,236 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 00:37:43
合計ジャッジ時間 2,471 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 58 ms
4,376 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 72 ms
4,376 KB
testcase_06 AC 128 ms
4,380 KB
testcase_07 AC 188 ms
4,376 KB
testcase_08 AC 116 ms
4,380 KB
testcase_09 AC 85 ms
4,376 KB
testcase_10 AC 11 ms
4,380 KB
testcase_11 AC 77 ms
4,384 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 24 ms
4,380 KB
testcase_14 AC 27 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(29): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`
Main.d(30): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

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

  int[] usedDigits()
  {
    bool[int] b;
    foreach (mi; mij)
      foreach (m; mi)
        b[m] = true;
    return b.keys;
  }

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

  bool calc(int d)
  {
    auto vij = new bool[][](h, w);

    point findDigit()
    {
      foreach (int i, mi; mij)
        foreach (int j, m; mi)
          if (m == d && !vij[i][j]) return point(j, i);
      return point(-1, -1);
    }

    bool wfs(point s)
    {
      auto q = SList!pointsCP(pointsCP(s, point(-1, -1)));
      vij[s.y][s.x] = true;

      while (!q.empty) {
        auto cp = q.front; q.removeFront;
        auto curr = cp.curr, prev = cp.prev;
        foreach (sp; sibPoints) {
          auto np = curr + sp;
          if (valid(np) && np != prev && mij[curr.y][curr.x] == mij[np.y][np.x]) {
            if (vij[np.y][np.x]) return true;
            vij[np.y][np.x] = true;
            q.insertFront(pointsCP(np, cp.curr));
          }
        }
      }
      return false;
    }

    for (;;) {
      auto s = findDigit;
      if (s.x == -1 || s.y == -1) return false;
      if (wfs(s)) return true;
    }
  }

  auto di = usedDigits;
  writeln(di.any!calc ? "possible" : "impossible");
}

struct pointsCP
{
  point curr;
  point prev;
}

struct Point(T) {
  T x, y;

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

alias Point!int point;

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