結果

問題 No.1572 XI
ユーザー tnakao0123tnakao0123
提出日時 2021-06-28 17:00:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 1,647 bytes
コンパイル時間 1,454 ms
コンパイル使用メモリ 96,432 KB
実行使用メモリ 28,212 KB
最終ジャッジ日時 2023-09-07 18:27:23
合計ジャッジ時間 6,582 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
26,976 KB
testcase_01 AC 9 ms
26,928 KB
testcase_02 AC 8 ms
26,948 KB
testcase_03 AC 8 ms
26,856 KB
testcase_04 AC 27 ms
27,376 KB
testcase_05 AC 104 ms
27,600 KB
testcase_06 AC 79 ms
27,752 KB
testcase_07 AC 12 ms
26,944 KB
testcase_08 AC 13 ms
27,656 KB
testcase_09 AC 46 ms
27,868 KB
testcase_10 AC 28 ms
27,884 KB
testcase_11 AC 14 ms
26,956 KB
testcase_12 AC 37 ms
27,872 KB
testcase_13 AC 15 ms
27,216 KB
testcase_14 AC 32 ms
27,512 KB
testcase_15 AC 10 ms
26,996 KB
testcase_16 AC 27 ms
27,584 KB
testcase_17 AC 11 ms
27,264 KB
testcase_18 AC 10 ms
27,720 KB
testcase_19 AC 24 ms
27,680 KB
testcase_20 AC 20 ms
27,788 KB
testcase_21 AC 83 ms
27,764 KB
testcase_22 AC 99 ms
27,640 KB
testcase_23 AC 9 ms
27,164 KB
testcase_24 AC 8 ms
26,920 KB
testcase_25 AC 8 ms
26,896 KB
testcase_26 AC 8 ms
27,008 KB
testcase_27 AC 8 ms
26,948 KB
testcase_28 AC 8 ms
27,048 KB
testcase_29 AC 8 ms
27,048 KB
testcase_30 AC 8 ms
26,944 KB
testcase_31 AC 9 ms
26,940 KB
testcase_32 AC 8 ms
26,904 KB
testcase_33 AC 8 ms
26,896 KB
testcase_34 AC 21 ms
27,888 KB
testcase_35 AC 14 ms
27,884 KB
testcase_36 AC 55 ms
27,972 KB
testcase_37 AC 188 ms
27,928 KB
testcase_38 AC 116 ms
28,036 KB
testcase_39 AC 85 ms
27,936 KB
testcase_40 AC 33 ms
28,076 KB
testcase_41 AC 114 ms
27,932 KB
testcase_42 AC 193 ms
27,920 KB
testcase_43 AC 78 ms
28,212 KB
testcase_44 AC 29 ms
27,912 KB
testcase_45 AC 139 ms
27,932 KB
testcase_46 AC 138 ms
27,920 KB
testcase_47 AC 12 ms
27,920 KB
testcase_48 AC 115 ms
27,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1572.cc:  No.1572 XI - 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_H = 1000;
const int MAX_W = 1000;
const int D = 6;
const int dxs[] = { 1, 0, -1, 0 }, dys[] = { 0, -1, 0, 1 };

const int rots[D][4] = {
  { 1, 2, 3, 4 },
  { 5, 1, 0, 1 },
  { 2, 5, 2, 0 },
  { 0, 3, 5, 3 },
  { 4, 0, 4, 5 },
  { 3, 4, 1, 2 },
};

/* typedef */

struct Stat {
  int y, x, d;
  Stat() {}
  Stat(int _y, int _x, int _d): y(_y), x(_x), d(_d) {}
};

/* global variables */

char fs[MAX_H][MAX_W + 4];
int ds[MAX_H][MAX_W][D];

/* subroutines */

/* main */

int main() {
  int h, w, sy, sx, gy, gx;
  scanf("%d%d%d%d%d%d", &h, &w, &sy, &sx, &gy, &gx);
  sy--, sx--, gy--, gx--;
  for (int y = 0; y < h; y++) scanf("%s", fs[y]);

  memset(ds, -1, sizeof(ds));
  ds[sy][sx][0] = 0;

  queue<Stat> q;
  q.push(Stat(sy, sx, 0));

  while (! q.empty()) {
    Stat u = q.front(); q.pop();
    if (u.y == gy && u.x == gx && u.d == 0) break;

    for (int di = 0; di < 4; di++) {
      int vy = u.y + dys[di], vx = u.x + dxs[di], vd = rots[u.d][di];
      if (vy >= 0 && vy < h && vx >= 0 && vx < w &&
	  fs[vy][vx] == '.' && ds[vy][vx][vd] < 0) {
	ds[vy][vx][vd] = ds[u.y][u.x][u.d] + 1;
	q.push(Stat(vy, vx, vd));
      }
    }
  }

  printf("%d\n", (ds[gy][gx][0] >= 0) ? ds[gy][gx][0] : -1);
  return 0;
}
0