結果

問題 No.367 ナイトの転身
ユーザー motimoti
提出日時 2016-04-29 23:21:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 2,599 bytes
コンパイル時間 1,165 ms
コンパイル使用メモリ 121,432 KB
実行使用メモリ 5,760 KB
最終ジャッジ日時 2024-04-19 07:38:35
合計ジャッジ時間 2,125 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,632 KB
testcase_01 AC 3 ms
5,504 KB
testcase_02 AC 3 ms
5,632 KB
testcase_03 AC 3 ms
5,632 KB
testcase_04 AC 3 ms
5,632 KB
testcase_05 AC 3 ms
5,504 KB
testcase_06 AC 3 ms
5,632 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,632 KB
testcase_09 AC 2 ms
5,504 KB
testcase_10 AC 14 ms
5,760 KB
testcase_11 AC 17 ms
5,760 KB
testcase_12 AC 5 ms
5,632 KB
testcase_13 AC 5 ms
5,760 KB
testcase_14 AC 11 ms
5,632 KB
testcase_15 AC 3 ms
5,504 KB
testcase_16 AC 13 ms
5,632 KB
testcase_17 AC 4 ms
5,632 KB
testcase_18 AC 5 ms
5,504 KB
testcase_19 AC 4 ms
5,504 KB
testcase_20 AC 3 ms
5,504 KB
testcase_21 AC 5 ms
5,632 KB
testcase_22 AC 3 ms
5,632 KB
testcase_23 AC 3 ms
5,632 KB
testcase_24 AC 3 ms
5,504 KB
testcase_25 AC 3 ms
5,632 KB
testcase_26 AC 3 ms
5,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

struct before_main{before_main(){cin.tie(0); ios::sync_with_stdio(false);}} before_main;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

template<class T> constexpr bool in_range(T y, T x, T H, T W) { return 0<=y&&y<H&&0<=x&&x<W; }

typedef long long ll;
int const inf = 1<<29;

int dxb[4] = {-1,+1,-1,+1};
int dyb[4] = {-1,-1,+1,+1};

int dxk[8] = {-2,-1,+1,+2,+2,+1,-1,-2};
int dyk[8] = {-1,-2,-2,-1,+1,+2,+2,+1};

int main() {

  int H, W; cin >> H >> W;
  vector<string> G(H);
  rep(i, H) {
    cin >> G[i];
  }

  int sy, sx;

  rep(i, H) rep(j, W) {
    if(G[i][j] == 'S') {
      sy = i, sx = j;
    }
  }

  queue<tuple<int, int, bool>> q;

  static int dist[505][505][2]; minus(dist);
  dist[sy][sx][0] = 0;

  q.emplace(sy, sx, 0);

  while(!q.empty()) {
    int y, x; bool mb; tie(y, x, mb) = q.front(); q.pop();

    if(G[y][x] == 'G') {
      cout << dist[y][x][mb] << endl;
      return 0;
    }

    if(mb) {
      rep(i, 4) {
        int ny = y + dyb[i], nx = x + dxb[i];
        if(!in_range(ny, nx, H, W)) { continue; }
        if(G[ny][nx] == 'R') {
          if(dist[ny][nx][0] >= 0) { continue; }
          dist[ny][nx][0] = dist[y][x][mb] + 1;
          q.emplace(ny, nx, 0);
        } else {
          if(dist[ny][nx][1] >= 0) { continue; }
          dist[ny][nx][1] = dist[y][x][mb] + 1;
          q.emplace(ny, nx, 1);
        }
      }
    } else {
      rep(i, 8) {
        int ny = y + dyk[i], nx = x + dxk[i];
        if(!in_range(ny, nx, H, W)) { continue; }
        if(G[ny][nx] == 'R') {
          if(dist[ny][nx][1] >= 0) { continue; }
          dist[ny][nx][1] = dist[y][x][mb] + 1;
          q.emplace(ny, nx, 1);
        } else {
          if(dist[ny][nx][0] >= 0) { continue; }
          dist[ny][nx][0] = dist[y][x][mb] + 1;
          q.emplace(ny, nx, 0);
        }
      }
    }
  }

  cout << -1 << endl;
  
  return 0;
}
0