結果

問題 No.13 囲みたい!
ユーザー te-shte-sh
提出日時 2016-09-01 13:33:30
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,644 bytes
コンパイル時間 649 ms
コンパイル使用メモリ 109,360 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-09-02 21:50:07
合計ジャッジ時間 1,747 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 4 ms
4,368 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 5 ms
4,368 KB
testcase_06 AC 3 ms
4,368 KB
testcase_07 AC 5 ms
4,372 KB
testcase_08 AC 5 ms
4,368 KB
testcase_09 AC 5 ms
4,368 KB
testcase_10 AC 2 ms
4,368 KB
testcase_11 AC 4 ms
4,372 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 AC 3 ms
4,372 KB
testcase_15 AC 2 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;
import std.string, std.conv, std.stdio, std.typecons;

struct point {
  int x;
  int 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)];

struct currPrev {
  point curr;
  point prev;
}

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

  writeln(calc(h, w, mij) ? "possible" : "impossible");
}

bool calc(int h, int w, int[][] mij)
{
  auto visited = new bool[][](h, w);
  foreach (i; 0..h) {
    foreach (j; 0..w) {
      if (!visited[i][j]) {
        auto r = calcN(i, j, h, w, mij, visited);
        if (r) return true;
      }
    }
  }
  return false;
}

bool calcN(int i, int j, int h, int w, int[][] mij, ref bool[][] visited)
{
  bool valid(point p) {
    return p.x >= 0 && p.x < w && p.y >= 0 && p.y < h;
  }

  auto stack = SList!currPrev();
  stack.insertFront(currPrev(point(j, i), point(-1, -1)));
  while (!stack.empty) {
    auto s = stack.front, cp = s.curr, pp = s.prev;
    stack.removeFront;

    foreach (sib; sibPoints) {
      auto np = cp + sib;

      if (!valid(np) || mij[np.y][np.x] != mij[cp.y][cp.x] || np == pp) continue;
      if (visited[np.y][np.x]) return true;

      stack.insertFront(currPrev(np, cp));
      visited[np.y][np.x] = true;
    }
  }

  return false;
}
0