結果

問題 No.340 雪の足跡
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-30 02:21:03
言語 D
(dmd 2.106.1)
結果
RE  
実行時間 -
コード長 3,704 bytes
コンパイル時間 960 ms
コンパイル使用メモリ 110,512 KB
実行使用メモリ 56,832 KB
最終ジャッジ日時 2023-09-03 23:19:06
合計ジャッジ時間 7,423 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 TLE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

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();
        }
      }
      */
      string[] tokens;
      tokens = readln.split;
      W = tokens[0].to!int;
      H = tokens[1].to!int;
      N = tokens[2].to!int;
      M = new int[N];
      B = new int[][N];
      foreach (i; 0 .. N) {
        M[i] = readln.chomp.to!int;
        B[i] = new int[M[i] + 1];
        tokens = readln.split;
        foreach (j; 0 .. M[i] + 1) {
          B[i][j] = tokens[j].to!int;
        }
      }
      
      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 = DList!int();
      auto dist = new int[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        dist[x][y] = -1;
      }
      dist[0][0] = 0;
      q.insertBack(0);
      q.insertBack(0);
      for (; !q.empty; ) {
        const x = q.front; q.removeFront;
        const y = q.front; q.removeFront;
        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) {
              if (dist[xx][yy] == -1) {
                dist[xx][yy] = dist[x][y] + 1;
                q.insertBack(xx);
                q.insertBack(yy);
              }
            }
          }
        }
      }
      if (dist[H - 1][W - 1] == -1) {
        writeln("Odekakedekinai..");
      } else {
        writeln(dist[H - 1][W - 1]);
      }
    }
  } catch (EOFException e) {
  }
}
0