結果

問題 No.13 囲みたい!
ユーザー te-shte-sh
提出日時 2020-01-21 11:28:06
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 4,835 bytes
コンパイル時間 1,399 ms
コンパイル使用メモリ 155,024 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 04:54:50
合計ジャッジ時間 2,421 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 9 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 9 ms
4,380 KB
testcase_08 AC 9 ms
4,376 KB
testcase_09 AC 9 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 7 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 4 ms
4,384 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// URL: https://yukicoder.me/problems/no/13

import std.algorithm, std.array, std.container, std.math, std.range, std.typecons, std.string;

version(unittest) {} else
void main()
{
  int W, H; io.getV(W, H);
  int[][] M; io.getM(H, W, M);

  auto g = grid(M), v = Grid!bool(W, H);
  alias P = g.P;

  auto hasCycle(P p)
  {
    struct P2 { P curr, prev; }
    auto q = DList!P2(P2(p, p)); v[p] = true;

    while (!q.empty) {
      auto e = q.front; q.removeFront();
      foreach (d; g.around4(e.curr).filter!(d => d != e.prev && g[d] == g[p])) {
	if (v[d]) return true;
	q.insertBack(P2(d, e.curr)); v[d] = true;
      }
    }

    return false;
  }

  foreach (y; 0..H)
    foreach (x; 0..W)
      if (!v[x, y] && hasCycle(P(x, y))) {
	io.put("possible");
	return;
      }

  io.put("impossible");
}

struct Point2(T)
{
  alias P = Point2!T, Op = string;
  T x, y;
  pure P opBinary(Op o)(P r) if (o=="+"||o=="-") {return mixin("P(x"~o~"r.x,y"~o~"r.y)");}
  pure P opBinary(Op o)(T a) if (o=="*"||o=="/") {return mixin("P(x"~o~"a,y"~o~"a)");}
  P opOpAssign(Op o)(P r) if (o=="+"||o=="-") {mixin("x"~o~"=r.x;y"~o~"=r.y;");return this;}
  P opOpAssign(Op o)(T a) if (o=="*"||o=="/") {mixin("x"~o~"=a;y"~o~"=a;");return this;}
  pure T opBinary(Op o: "*")(P r) {return x*r.x+y*r.y;}
  pure T hypot2() {return x^^2+y^^2;}
}

pure T cross(T)(Point2!T p1, Point2!T p2)
{
  return p1.x*p2.y - p1.y*p2.x;
}

struct Point3(T)
{
  alias P = Point3!T, Op = string;
  T x, y, z;
  pure P opBinary(Op o)(P r) if (o=="+"||o=="-") {return mixin("P(x"~o~"r.x,y"~o~"r.y,z"~o~"r.z)");}
  pure P opBinary(Op o)(T a) if (o=="*"||o=="/") {return mixin("P(x"~o~"a,y"~o~"a,z"~o~"a)");}
  P opOpAssign(Op o)(P r) if (o=="+"||o=="-") {mixin("x"~o~"=r.x;y"~o~"=r.y;z"~o~"=r.z;");return this;}
  P opOpAssign(Op o)(T a) if (o=="*"||o=="/") {mixin("x"~o~"=a;y"~o~"=a;z"~o~"=a;");return this;}
  pure T opBinary(Op o: "*")(P r) {return x*r.x+y*r.y+z*r.z;}
  pure T hypot2() {return x^^2+y^^2+z^^2;}
}

pure Point3!T cross(T)(Point3!T p1, Point3!T p2)
{
  return Point3!T(p1.y*p2.z - p1.z*p2.y, p1.z*p2.x - p1.x*p2.z, p1.x*p2.y - p1.y*p2.x);
}

struct Grid(T)
{
  alias G = Grid!T, P = Point2!int;
  size_t c, r;
  T[][] data;

  this(size_t c, size_t r) { this.c = c; this.r = r; data = new T[][](r, c); }
  this(T[][] data) { c = data[0].length; r = data.length; this.data = data; }

  pure T opIndex(size_t x, size_t y) { return data[y][x]; }
  pure T opIndex(P p) { return data[p.y][p.x]; }
  G opIndexAssign(T v, size_t x, size_t y) { data[y][x] = v; return this; }
  G opIndexAssign(T v, P p) { data[p.y][p.x] = v; return this; }
  G opIndexOpAssign(string op)(T v, size_t x, size_t y) { mixin("data[y][x]"~op~"=v;"); return this; }
  G opIndexOpAssign(string op)(T v, P p) { mixin("data[p.y][p.x]"~op~"=v;"); return this; }

  pure bool valid(size_t x, size_t y) { return 0 <= x && x < c && 0 <= y && y < r; }
  pure bool valid(P p) { return valid(p.x, p.y); }
  auto d4 = [P(-1, 0), P(0, -1), P(1, 0), P(0, 1)];
  pure auto around4(P p) { return d4.map!(d => d+p).filter!(np => valid(np)); }
  auto d8 = [P(-1, 0), P(-1, -1), P(0, -1), P(1, -1), P(1, 0), P(1, 1), P(0, 1), P(-1, 1)];
  pure auto around8(P p) { return d8.map!(d => d+p).filter!(np => valid(np)); }
}
Grid!T grid(T)(T[][] data) { return Grid!T(data); }

auto io = IO!()();
import std.stdio;
struct IO(alias IN = stdin, alias OUT = stdout, string delimiter = " ", string floatFormat = "%.10f")
{
  import std.conv, std.format, std.meta, std.traits;
  alias assignable = hasAssignableElements;

  dchar[] buf;
  auto sp = (new dchar[](0)).splitter;
  void nextLine() { IN.readln(buf); sp = buf.splitter; }

  auto get(T)(ref T v) { if (sp.empty) nextLine(); v = sp.front.to!T; sp.popFront(); }
  auto getV(T...)(ref T v) { foreach (ref w; v) get(w); }
  auto getA(T)(size_t n, ref T v) if (assignable!T) { v = new T(n); foreach (ref w; v) get(w); }
  auto getC(T...)(size_t n, ref T v) if (allSatisfy!(assignable, T))
  {
    foreach (ref w; v) w = new typeof(w)(n);
    foreach (i; 0..n) foreach (ref w; v) get(w[i]);
  }
  auto getM(T)(size_t r, size_t c, ref T v) if (assignable!T && assignable!(ElementType!T))
  {
    v = new T(r); foreach (ref w; v) getA(c, w);
  }

  auto putR(T)(T v)
  {
    auto w = v;
    while (!w.empty) { putA(w.front); w.popFront(); if (!w.empty) OUT.write(delimiter); }
  }
  auto putA(T)(T v)
  {
    static if (isInputRange!T && !isSomeString!T) putR(v);
    else if (isFloatingPoint!T) OUT.write(format(floatFormat, v));
    else OUT.write(v);
  }
  auto put(bool flush = false, T...)(T v)
  {
    foreach (i, w; v) { putA(w); if (i < v.length-1) OUT.write(delimiter); }
    OUT.writeln;
    static if (flush) OUT.flush();
  }

  auto putB(S, T)(bool c, S t, T f) { if (c) put(t); else put(f); }
  auto putRaw(T...)(T v) { OUT.write(v); OUT.writeln; }
}
0