結果

問題 No.340 雪の足跡
ユーザー ei1333333ei1333333
提出日時 2016-02-18 12:31:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 462 ms / 1,000 ms
コード長 1,445 bytes
コンパイル時間 1,358 ms
コンパイル使用メモリ 167,540 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2023-10-23 18:04:46
合計ジャッジ時間 10,077 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,552 KB
testcase_01 AC 4 ms
7,556 KB
testcase_02 AC 4 ms
7,560 KB
testcase_03 AC 5 ms
7,552 KB
testcase_04 AC 5 ms
7,552 KB
testcase_05 AC 5 ms
7,540 KB
testcase_06 AC 4 ms
7,560 KB
testcase_07 AC 5 ms
7,544 KB
testcase_08 AC 4 ms
7,556 KB
testcase_09 AC 5 ms
7,572 KB
testcase_10 AC 5 ms
7,564 KB
testcase_11 AC 6 ms
7,652 KB
testcase_12 AC 5 ms
7,816 KB
testcase_13 AC 6 ms
7,792 KB
testcase_14 AC 24 ms
10,092 KB
testcase_15 AC 28 ms
10,124 KB
testcase_16 AC 22 ms
10,048 KB
testcase_17 AC 35 ms
10,128 KB
testcase_18 AC 29 ms
10,128 KB
testcase_19 AC 36 ms
10,128 KB
testcase_20 AC 304 ms
15,352 KB
testcase_21 AC 197 ms
14,536 KB
testcase_22 AC 11 ms
15,352 KB
testcase_23 AC 365 ms
15,352 KB
testcase_24 AC 287 ms
15,352 KB
testcase_25 AC 316 ms
15,352 KB
testcase_26 AC 53 ms
7,828 KB
testcase_27 AC 14 ms
7,732 KB
testcase_28 AC 47 ms
10,088 KB
testcase_29 AC 379 ms
14,360 KB
testcase_30 AC 260 ms
14,360 KB
testcase_31 AC 394 ms
15,276 KB
testcase_32 AC 462 ms
15,360 KB
testcase_33 AC 357 ms
15,360 KB
testcase_34 AC 458 ms
15,360 KB
testcase_35 AC 341 ms
15,360 KB
testcase_36 AC 340 ms
15,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int W, H, N;
int isgo[1000][1000][2];

int bfs()
{
  queue< pair< int, int > > que;
  int min_cost[1000][1000];
  const static int vx[] = {0, 1, 0, -1}, vy[] = {1, 0, -1, 0};

  memset(min_cost, -1, sizeof(min_cost));
  que.push({0, 0});
  min_cost[0][0] = 0;
  while(!que.empty()) {
    int x, y;
    tie(x, y) = que.front(); que.pop();
    if(x == W - 1 && y == H - 1) return(min_cost[x][y]);
    for(int i = 0; i < 4; i++) {
      int nx = x + vx[i], ny = y + vy[i];
      if(nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
      if(!isgo[min(x, nx)][min(y, ny)][ny == y]) continue;
      if(min_cost[nx][ny] == -1) {
        min_cost[nx][ny] = min_cost[x][y] + 1;
        que.push({nx, ny});
      }
    }
  }
  return(-1);
}

int main()
{
  cin >> W >> H >> N;
  for(int i = 0; i < N; i++) {
    int M, B[2];
    cin >> M;
    cin >> B[1];
    for(int j = 0; j < M; j++) {
      cin >> B[j & 1];
      const int U = min(B[0], B[1]), V = max(B[0], B[1]);
      const int X1 = U % W, Y1 = U / W;
      const int X2 = V % W, Y2 = V / W;
      ++isgo[X1][Y1][Y1 == Y2];
      --isgo[X2][Y2][Y1 == Y2];
    }
  }
  
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      if(i > 0) isgo[j][i][0] += isgo[j][i - 1][0];
      if(j > 0) isgo[j][i][1] += isgo[j - 1][i][1];
    }
  }

  int ret = bfs();
  if(ret == -1) cout << "Odekakedekinai.." << endl;
  else cout << ret << endl;
}
0