結果

問題 No.340 雪の足跡
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-30 02:53:19
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 283 ms / 1,000 ms
コード長 4,031 bytes
コンパイル時間 1,312 ms
コンパイル使用メモリ 106,868 KB
実行使用メモリ 32,780 KB
最終ジャッジ日時 2023-09-03 23:20:46
合計ジャッジ時間 5,644 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 15 ms
7,468 KB
testcase_15 AC 18 ms
7,464 KB
testcase_16 AC 13 ms
4,676 KB
testcase_17 AC 22 ms
7,188 KB
testcase_18 AC 19 ms
6,908 KB
testcase_19 AC 23 ms
7,232 KB
testcase_20 AC 189 ms
31,816 KB
testcase_21 AC 113 ms
14,964 KB
testcase_22 AC 22 ms
26,560 KB
testcase_23 AC 200 ms
31,836 KB
testcase_24 AC 168 ms
32,708 KB
testcase_25 AC 177 ms
32,780 KB
testcase_26 AC 30 ms
7,624 KB
testcase_27 AC 8 ms
4,380 KB
testcase_28 AC 28 ms
8,056 KB
testcase_29 AC 228 ms
27,184 KB
testcase_30 AC 157 ms
19,564 KB
testcase_31 AC 246 ms
29,352 KB
testcase_32 AC 280 ms
32,572 KB
testcase_33 AC 225 ms
32,136 KB
testcase_34 AC 283 ms
32,708 KB
testcase_35 AC 207 ms
32,700 KB
testcase_36 AC 213 ms
31,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int H, W, N;
int[] M;
int[][] B;

void main() {
  try {
    for (; ; ) {
      W = readInt();
      H = readInt();
      N = readInt();
      M = new int[N];
      B = new int[][N];
      foreach (i; 0 .. N) {
        M[i] = readInt();
        B[i] = new int[M[i] + 1];
        foreach (j; 0 .. M[i] + 1) {
          B[i][j] = readInt();
        }
      }
      
      auto road = new int[][](H * 2, W * 2);
      foreach (i; 0 .. N) {
        foreach (j; 0 .. M[i]) {
          int xa = B[i][j] / W;
          int ya = B[i][j] % W;
          int xb = B[i][j + 1] / W;
          int yb = B[i][j + 1] % W;
          if (xa > xb) {
            swap(xa, xb);
          }
          if (ya > yb) {
            swap(ya, yb);
          }
          ++road[xa * 2][ya * 2];
          --road[xa * 2][yb * 2 + 1];
          --road[xb * 2 + 1][ya * 2];
          ++road[xb * 2 + 1][yb * 2 + 1];
        }
      }
      foreach (x; 1 .. H * 2) foreach (y; 0 .. W * 2) {
        road[x][y] += road[x - 1][y];
      }
      foreach (x; 0 .. H * 2) foreach (y; 1 .. W * 2) {
        road[x][y] += road[x][y - 1];
      }
      debug {
        foreach_reverse (x; 0 .. H * 2) {
          writeln(road[x]);
        }
      }
      
      auto q = new int[H * W];
      int qHead, qTail;
      auto dist = new int[H * W];
      dist[] = -1;
      dist[0] = 0;
      q[qTail++] = 0;
      for (; qHead < qTail; ) {
        const u = q[qHead];
        ++qHead;
        const x = u / W;
        const y = u % W;
        /*
        foreach (dir; 0 .. 4) {
          const xx = x + [+1, 0, -1, 0][dir];
          const yy = y + [0, +1, 0, -1][dir];
          if (0 <= xx && xx < H && 0 <= yy && yy < W) {
            if (road[x + xx][y + yy] > 0) {
              const v = xx * W + yy;
              if (dist[v] == -1) {
                dist[v] = dist[u] + 1;
                q[qTail++] = v;
              }
            }
          }
        }
        */
        if (x + 1 < H && road[x + x + 1][y + y] > 0) {
          const v = u + W;
          if (dist[v] == -1) {
            dist[v] = dist[u] + 1;
            q[qTail++] = v;
          }
        }
        if (y + 1 < W && road[x + x][y + y + 1] > 0) {
          const v = u + 1;
          if (dist[v] == -1) {
            dist[v] = dist[u] + 1;
            q[qTail++] = v;
          }
        }
        if (x - 1 >= 0 && road[x + x - 1][y + y] > 0) {
          const v = u - W;
          if (dist[v] == -1) {
            dist[v] = dist[u] + 1;
            q[qTail++] = v;
          }
        }
        if (y - 1 >= 0 && road[x + x][y + y - 1] > 0) {
          const v = u - 1;
          if (dist[v] == -1) {
            dist[v] = dist[u] + 1;
            q[qTail++] = v;
          }
        }
      }
      if (dist[H * W - 1] == -1) {
        writeln("Odekakedekinai..");
      } else {
        writeln(dist[H * W - 1]);
      }
    }
  } catch (EOFException e) {
  }
}
0