結果

問題 No.340 雪の足跡
ユーザー tnakao0123tnakao0123
提出日時 2016-04-07 01:07:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 468 ms / 1,000 ms
コード長 2,440 bytes
コンパイル時間 1,062 ms
コンパイル使用メモリ 92,264 KB
実行使用メモリ 23,184 KB
最終ジャッジ日時 2024-04-14 22:22:14
合計ジャッジ時間 8,055 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 4 ms
6,944 KB
testcase_12 AC 4 ms
8,008 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 22 ms
10,700 KB
testcase_15 AC 27 ms
12,468 KB
testcase_16 AC 21 ms
8,392 KB
testcase_17 AC 36 ms
10,952 KB
testcase_18 AC 29 ms
12,420 KB
testcase_19 AC 36 ms
10,828 KB
testcase_20 AC 313 ms
23,184 KB
testcase_21 AC 194 ms
6,940 KB
testcase_22 AC 17 ms
22,996 KB
testcase_23 AC 357 ms
23,184 KB
testcase_24 AC 298 ms
23,056 KB
testcase_25 AC 327 ms
23,056 KB
testcase_26 AC 53 ms
12,340 KB
testcase_27 AC 12 ms
6,944 KB
testcase_28 AC 48 ms
10,824 KB
testcase_29 AC 396 ms
22,364 KB
testcase_30 AC 270 ms
16,056 KB
testcase_31 AC 393 ms
21,996 KB
testcase_32 AC 468 ms
23,184 KB
testcase_33 AC 353 ms
23,056 KB
testcase_34 AC 459 ms
23,180 KB
testcase_35 AC 342 ms
23,180 KB
testcase_36 AC 340 ms
22,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 340.cc: No.340 雪の足跡 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_W = 1000;
const int MAX_H = 1000;

const int dxs[] = {1, 0, -1, 0}, dys[] = {0, 1, 0, -1};

const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int,int> pii;

/* global variables */

int nbrs[MAX_H][MAX_W][4], dists[MAX_H][MAX_W];

/* subroutines */

/* main */

int main() {
  int w, h, n;
  cin >> w >> h >> n;

  while (n--) {
    int m, b0;
    cin >> m >> b0;

    int x0 = b0 % w, y0 = b0 / w;
    while (m--) {
      int b1;
      cin >> b1;
      int x1 = b1 % w, y1 = b1 / w;
      //printf("(%d,%d)->(%d,%d)\n", x0, y0, x1, y1);

      if (x0 < x1)
	nbrs[y0][x0][0]++, nbrs[y0][x1][0]--;
      else if (y0 < y1)
	nbrs[y0][x0][1]++, nbrs[y1][x0][1]--;
      else if (x0 > x1)
	nbrs[y0][x1][0]++, nbrs[y0][x0][0]--;
      else 
	nbrs[y1][x0][1]++, nbrs[y0][x0][1]--;

      x0 = x1, y0 = y1;
    }
  }

  for (int y = 0; y < h; y++)
    for (int x = 1; x < w; x++) nbrs[y][x][0] += nbrs[y][x - 1][0];
  for (int x = 0; x < w; x++)
    for (int y = 1; y < h; y++) nbrs[y][x][1] += nbrs[y - 1][x][1];
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++) {
      if (nbrs[y][x][0]) nbrs[y][x + 1][2] = true;
      if (nbrs[y][x][1]) nbrs[y + 1][x][3] = true;
    }
      
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++) dists[y][x] = INF;
  dists[0][0] = 0;

  queue<pii> q;
  q.push(pii(0, 0));

  while (! q.empty()) {
    pii u = q.front(); q.pop();
    int ux = u.first, uy = u.second;
    //printf("x=%d,y=%d,d=%d\n", ux, uy, dists[uy][ux]);

    if (ux == w - 1 && uy == h - 1) break;

    int vd = dists[uy][ux] + 1;

    for (int di = 0; di < 4; di++)
      if (nbrs[uy][ux][di]) {
	int vx = ux + dxs[di], vy = uy + dys[di];
	//printf("  (%d,%d)[%d]->(%d,%d):%d\n", ux, uy, di, vx, vy, dists[vy][vx]);
	if (dists[vy][vx] > vd) {
	  dists[vy][vx] = vd;
	  q.push(pii(vx, vy));
	}
      }
  }

  int gd = dists[h - 1][w - 1];
  if (gd >= INF)
    puts("Odekakedekinai..");
  else
    printf("%d\n", gd);

  return 0;
}
0