結果

問題 No.367 ナイトの転身
ユーザー ei1333333ei1333333
提出日時 2016-04-29 23:23:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,326 bytes
コンパイル時間 1,432 ms
コンパイル使用メモリ 165,948 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 05:56:53
合計ジャッジ時間 2,666 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 28 ms
6,940 KB
testcase_11 AC 36 ms
6,944 KB
testcase_12 AC 10 ms
6,940 KB
testcase_13 AC 10 ms
6,944 KB
testcase_14 AC 15 ms
6,940 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 15 ms
6,944 KB
testcase_17 AC 5 ms
6,940 KB
testcase_18 AC 5 ms
6,940 KB
testcase_19 AC 6 ms
6,944 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 6 ms
6,940 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 4 ms
6,940 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:48:26: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   48 |   cout << bfs(sx, sy, 'G') << endl;
      |                          ^
main.cpp:48:26: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

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

int W, H;
char mas[500][500];
int min_cost[2][500][500];
 
int bfs(int sx, int sy, char c)
{
  static const int vx[] = {-1, 1, -2, 2, -2, 2, -1, 1}, vy[] = {-2, -2, -1, -1, 1, 1, 2, 2};
  static const int dx[] = {-1, 1, -1, 1}, dy[] = {-1, -1, 1, 1};
  
  memset(min_cost, -1, sizeof(min_cost));
   
  queue< pair< bool, pair< int, int > > > que;
  que.push({0, {sx, sy}});
  min_cost[0][sx][sy] = 0;
 
  while(!que.empty()) {
    auto p = que.front().second;
    int type = que.front().first;
    que.pop();
    if(mas[p.second][p.first] == c) return(min_cost[type][p.first][p.second]);
    for(int i = 0; i < (type ? 4 : 8); i++) {
      int nx = p.first + (type ? dx[i] : vx[i]);
      int ny = p.second + (type ? dy[i] : vy[i]);
      if(nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
      bool flip = mas[ny][nx] == 'R';
      if(min_cost[type ^ flip][nx][ny] != -1) continue;
      min_cost[type ^ flip][nx][ny] = min_cost[type][p.first][p.second] + 1;
      que.push({type ^ flip, {nx, ny}});
    }
  }
  return(-1);
}

int main()
{
  cin >> H >> W;
  int sx, sy;
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      cin >> mas[i][j];
      if(mas[i][j] == 'S') sx = j, sy = i;
    }
  }
  cout << bfs(sx, sy, 'G') << endl;
}
0