結果

問題 No.367 ナイトの転身
ユーザー tnakao0123tnakao0123
提出日時 2016-05-02 01:44:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,970 bytes
コンパイル時間 905 ms
コンパイル使用メモリ 89,736 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 08:41:25
合計ジャッジ時間 1,817 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 18 ms
6,940 KB
testcase_11 AC 24 ms
6,940 KB
testcase_12 AC 6 ms
6,940 KB
testcase_13 AC 6 ms
6,944 KB
testcase_14 AC 10 ms
6,944 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 10 ms
6,940 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 4 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:81:5: warning: ‘gx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   81 |     if (u.x == gx && u.y == gy) {
      |     ^~
main.cpp:81:19: warning: ‘gy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   81 |     if (u.x == gx && u.y == gy) {
      |         ~~~~~~~~~~^~~~~~~~~~~~
main.cpp:42:33: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   42 |   Stat(int _x, int _y, int _k): x(_x), y(_y), k(_k) {}
      |                                 ^~~~~
main.cpp:58:7: note: ‘sx’ was declared here
   58 |   int sx, sy, gx, gy;
      |       ^~
main.cpp:42:40: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   42 |   Stat(int _x, int _y, int _k): x(_x), y(_y), k(_k) {}
      |                                        ^~~~~
main.cpp:58:11: note: ‘sy’ was declared here
   58 |   int sx, sy, gx, gy;
      |           ^~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 367.cc: No.367 ナイトの転身 - 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 = 500;
const int MAX_W = 500;
const int INF = 1 << 30;

const int dns[2] = {8, 4};
const int dxs[2][8] = { {2, 1, -1, -2, -2, -1, 1, 2}, {1, -1, -1, 1} };
const int dys[2][8] = { {-1, -2, -2, -1, 1, 2, 2, 1}, {-1, -1, 1, 1} };

/* typedef */

struct Stat {
  int x, y, k;
  Stat() {}
  Stat(int _x, int _y, int _k): x(_x), y(_y), k(_k) {}
};

/* global variables */

string flds[MAX_H];
int dists[MAX_H][MAX_W][2];

/* subroutines */

/* main */

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

  int sx, sy, gx, gy;
  
  for (int y = 0; y < h; y++) {
    cin >> flds[y];
    for (int x = 0; x < w; x++) {
      switch (flds[y][x]) {
      case 'S': sx = x; sy = y; flds[y][x] = '.'; break;
      case 'G': gx = x; gy = y; flds[y][x] = '.'; break;
      }
    }
  }

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      for (int k = 0; k < 2; k++) dists[y][x][k] = INF;
  dists[sy][sx][0] = 0;

  queue<Stat> q;
  q.push(Stat(sx, sy, 0));
  int mind = INF;
  
  while (! q.empty()) {
    Stat u = q.front(); q.pop();
    if (u.x == gx && u.y == gy) {
      mind = dists[u.y][u.x][u.k];
      break;
    }

    int vd = dists[u.y][u.x][u.k] + 1;
    int vk = (flds[u.y][u.x] == 'R') ? (u.k ^ 1) : u.k;

    for (int di = 0; di < dns[vk]; di++) {
      int vx = u.x + dxs[vk][di], vy = u.y + dys[vk][di];
      if (vx >= 0 && vx < w && vy >= 0 && vy < h &&
	  dists[vy][vx][vk] > vd) {
	dists[vy][vx][vk] = vd;
	q.push(Stat(vx, vy, vk));
      }
    }
  }

  printf("%d\n", (mind >= INF) ? -1 : mind);
  return 0;
}
0