結果

問題 No.367 ナイトの転身
ユーザー しらっ亭しらっ亭
提出日時 2016-05-02 01:49:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,720 bytes
コンパイル時間 1,801 ms
コンパイル使用メモリ 171,404 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 08:41:56
合計ジャッジ時間 2,840 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 16 ms
5,376 KB
testcase_11 AC 24 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 13 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 14 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
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 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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;
      if (ny < 0 || ny >= H) continue;
      if (nx < 0 || nx >= W) continue;
      int nt = S[ny][nx] == 'R' ? !t : t;
      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