結果

問題 No.367 ナイトの転身
ユーザー しらっ亭しらっ亭
提出日時 2016-05-02 01:48:16
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,720 bytes
コンパイル時間 1,951 ms
コンパイル使用メモリ 172,168 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 08:41:53
合計ジャッジ時間 5,966 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FORR(x,arr) for(auto&& x:arr)
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i,n) for (int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define BIT(n) (1LL<<(n))
#define SZ(x) ((int)(x).size())
typedef long long ll;
// -------------------------------------

typedef tuple<int, int, int, int> IIII;

int H, W;
string S[500];
int sx, sy, gx, gy;

bool seen[500][500][2];

vector<vector<int>> DY;
vector<vector<int>> DX;

void Main() {
  DY.push_back({-2,-1,1,2,2,1,-1,-2});
  DX.push_back({1,2,2,1,-1,-2,-2,-1});
  DY.push_back({-1,1,1,-1});
  DX.push_back({1,1,-1,-1});

  cin >> H >> W;
  REP(i, H) {
    cin >> S[i];
    REP(j, W) {
      if (S[i][j] == 'S') sy=i, sx=j;
      else if (S[i][j] == 'G') gy=i, gx=j;
    }
  }

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

  while(!q.empty()) {
    int n = get<0>(q.front());
    int t = get<1>(q.front());
    int y = get<2>(q.front());
    int x = get<3>(q.front());
    q.pop();

    if (x == gx && y == gy) {
      _P("%d\n", n);
      return;
    }

    int l = SZ(DY[t]);
    REP(i, l) {
      int dy = DY[t][i];
      int dx = DX[t][i];
      int ny = y + dy;
      int nx = x + dx;
      int nt = S[ny][nx] == 'R' ? !t : t;
      if (ny < 0 || ny >= H) continue;
      if (nx < 0 || nx >= W) continue;
      if (seen[ny][nx][nt]) continue;
      q.push(make_tuple(n+1, nt, ny, nx));
      seen[ny][nx][nt] = 1;
    }
  }
  puts("-1");
}

int main() { cin.tie(0); ios::sync_with_stdio(false); Main(); return 0; }
0