結果

問題 No.367 ナイトの転身
ユーザー saltcandy123saltcandy123
提出日時 2016-04-29 23:26:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,267 bytes
コンパイル時間 837 ms
コンパイル使用メモリ 75,020 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 05:57:57
合計ジャッジ時間 1,740 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 2 ms
6,940 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 3 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 24 ms
6,940 KB
testcase_11 AC 36 ms
6,944 KB
testcase_12 AC 7 ms
6,940 KB
testcase_13 AC 7 ms
6,944 KB
testcase_14 AC 16 ms
6,944 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 17 ms
6,940 KB
testcase_17 AC 4 ms
6,940 KB
testcase_18 AC 6 ms
6,944 KB
testcase_19 AC 5 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 6 ms
6,944 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  vector<vector<int>> dx = {{1, -1, 1, -1, 2, -2, 2, -2}, {1, -1, 1, -1}};
  vector<vector<int>> dy = {{2, 2, -2, -2, 1, 1, -1, -1}, {1, 1, -1, -1}};
  int h, w;
  int count[500][500][2];
  vector<string> area;
  queue<vector<int>> q;
  cin >> h >> w;
  area.resize(h);
  for (int y = 0; y < h; ++y) {
    cin >> area[y];
    for (int x = 0; x < w; ++x) {
      count[y][x][0] = count[y][x][1] = -1;
      if (area[y][x] == 'S') {
        count[y][x][0] = 0;
        q.push(vector<int>{0, x, y});
      }
    }
  }
  while (not q.empty()) {
    int s = q.front()[0];
    int x = q.front()[1];
    int y = q.front()[2];
    q.pop();
    for (size_t d = 0; d < dx[s].size(); ++d) {
      int nx = x + dx[s][d];
      int ny = y + dy[s][d];
      if (0 <= nx and nx < w and 0 <= ny and ny < h) {
        int ns = (area[ny][nx] == 'R'? 1 - s: s);
        if (count[ny][nx][ns] < 0) {
          count[ny][nx][ns] = count[y][x][s] + 1;
          q.push({ns, nx, ny});
        }
        if (area[ny][nx] == 'G') {
          cout << count[y][x][s] + 1 << endl;
          return 0;
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
0