結果

問題 No.367 ナイトの転身
ユーザー saltcandy123
提出日時 2016-04-29 23:26:14
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,267 bytes
コンパイル時間 689 ms
コンパイル使用メモリ 74,948 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-04 19:03:10
合計ジャッジ時間 1,544 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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