結果

問題 No.1572 XI
ユーザー tnakao0123
提出日時 2021-06-28 17:00:21
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 208 ms / 2,000 ms
コード長 1,647 bytes
コンパイル時間 870 ms
コンパイル使用メモリ 99,332 KB
実行使用メモリ 28,204 KB
最終ジャッジ日時 2024-06-25 12:11:56
合計ジャッジ時間 4,979 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

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