結果

問題 No.367 ナイトの転身
ユーザー roiti46roiti46
提出日時 2016-04-30 00:01:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 1,942 bytes
コンパイル時間 1,470 ms
コンパイル使用メモリ 170,372 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 06:16:19
合計ジャッジ時間 2,585 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 48 ms
6,944 KB
testcase_11 AC 95 ms
6,940 KB
testcase_12 AC 9 ms
6,944 KB
testcase_13 AC 10 ms
6,940 KB
testcase_14 AC 26 ms
6,944 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 27 ms
6,944 KB
testcase_17 AC 5 ms
6,944 KB
testcase_18 AC 7 ms
6,944 KB
testcase_19 AC 6 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 6 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,940 KB
testcase_26 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T> using Graph = vector<vector<T>>;

#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i: a)
#define ITR(x, a) for (__typeof(a.begin()) x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define endl '\n'

int dx[] = {2, 2, 1, 1, -1, -1, -2, -2};
int dy[] = {1, -1, 2, -2, 2, -2, 1, -1};

int dx2[] = {1, -1, -1, 1};
int dy2[] = {1, 1, -1, -1};

const int inf = 1e8;

int H, W;
int mn[505][505][2];
string s[505];

bool is_inside(int x, int y) {
  return (0 <= x && x < W && 0 <= y && y < H);
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> H >> W;
  rep(i, H) cin >> s[i];
  
  int sx = 0, sy = 0, gx = 0, gy = 0;
  rep(y, H) rep(x, W) {
    mn[y][x][0] = mn[y][x][1] = inf;
    if (s[y][x] == 'S') sx = x, sy = y;
    if (s[y][x] == 'G') gx = x, gy = y;
  }

  int ans = -1;
  priority_queue<tuple<int, int, int, int>> que;
  que.push(make_tuple(0, sx, sy, 0));

  while (!que.empty()) {
    auto ele = que.top(); que.pop();
    int cost = -get<0>(ele), x = get<1>(ele), y = get<2>(ele), z = get<3>(ele);

    if (x == gx && y == gy) {
      ans = cost;
      break;
    }

    if (s[y][x] == 'R') z = 1 - z;

    if (z == 0) {
      rep(i, 8) {
        int nx = x + dx[i], ny = y + dy[i];
        if (!is_inside(nx, ny)) continue;
        if (mn[ny][nx][z] <= cost + 1) continue;
        mn[ny][nx][z] = cost + 1;
        que.push(make_tuple(-(cost + 1), nx, ny, z));
      }
    } else {
      rep(i, 4) {
        int nx = x + dx2[i], ny = y + dy2[i];
        if (!is_inside(nx, ny)) continue;
        if (mn[ny][nx][z] <= cost + 1) continue;
        mn[ny][nx][z] = cost + 1;
        que.push(make_tuple(-(cost + 1), nx, ny, z));
      }
    }
  }
  
  cout << ans << endl;

  return 0;
}
0