結果
| 問題 | No.367 ナイトの転身 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-04-30 00:23:27 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 29 ms / 2,000 ms | 
| コード長 | 1,827 bytes | 
| コンパイル時間 | 955 ms | 
| コンパイル使用メモリ | 91,452 KB | 
| 実行使用メモリ | 6,784 KB | 
| 最終ジャッジ日時 | 2024-10-11 00:19:41 | 
| 合計ジャッジ時間 | 1,955 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:30:17: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
   30 |     dp[0][gy][gx] = 0;
      |                 ^
main.cpp:19:13: note: 'gx' was declared here
   19 |     int gy, gx, sy, sx;
      |             ^~
main.cpp:30:13: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
   30 |     dp[0][gy][gx] = 0;
      |             ^
main.cpp:19:9: note: 'gy' was declared here
   19 |     int gy, gx, sy, sx;
      |         ^~
main.cpp:48:46: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
   48 |     int ans = min(dp[0][sy][sx], dp[1][sy][sx]);
      |                                              ^
main.cpp:19:21: note: 'sx' was declared here
   19 |     int gy, gx, sy, sx;
      |                     ^~
main.cpp:48:42: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
   48 |     int ans = min(dp[0][sy][sx], dp[1][sy][sx]);
      |                                          ^
main.cpp:19:17: note: 'sy' was declared here
   19 |     int gy, gx, sy, sx;
      |                 ^~
            
            ソースコード
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
using namespace std;
const vector<int> knight_move_y { 1, 2, 2, 1, -1, -2, -2, -1 };
const vector<int> knight_move_x { 2, 1, -1, -2, -2, -1, 1, 2 };
const vector<int> bishop_move_y { 1, 1, -1, -1 };
const vector<int> bishop_move_x { 1, -1, -1, 1 };
const int inf = 1e9+7;
int main() {
    // input
    int h, w; cin >> h >> w;
    vector<string> s(h); repeat (y,h) cin >> s[y];
    // prepare
    auto on_field = [&](int y, int x) { return 0 <= y and y < h and 0 <= x and x < w; };
    int gy, gx, sy, sx;
    repeat (y,h) repeat (x,w) {
        switch (s[y][x]) {
            case 'S': sy = y; sx = x; break;
            case 'G': gy = y; gx = x; break;
        }
    }
    // bfs
    vector<vector<vector<int> > > dp(2, vector<vector<int> >(h, vector<int>(w, inf)));
    queue<tuple<bool,int,int> > que;
    que.push(make_tuple(false, gy, gx));
    dp[0][gy][gx] = 0;
    while (not que.empty()) {
        bool bishop; int y, x; tie(bishop, y, x) = que.front(); que.pop();
        vector<int> const & dy = bishop ? bishop_move_y : knight_move_y;
        vector<int> const & dx = bishop ? bishop_move_x : knight_move_x;
        int len = dy.size();
        repeat (i,len) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (not on_field(ny, nx)) continue;
            bool nbishop = s[ny][nx] == 'R' ? not bishop : bishop;
            if (dp[nbishop][ny][nx] == inf) {
                dp[nbishop][ny][nx] = dp[bishop][y][x] + 1;
                que.push(make_tuple(nbishop, ny, nx));
            }
        }
    }
    // output
    int ans = min(dp[0][sy][sx], dp[1][sy][sx]);
    if (ans == inf) ans = -1;
    cout << ans << endl;
    return 0;
}
            
            
            
        