結果

問題 No.367 ナイトの転身
コンテスト
ユーザー t98slider
提出日時 2023-03-23 15:55:37
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 1,372 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,376 ms
コンパイル使用メモリ 201,728 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-06 17:38:59
合計ジャッジ時間 2,270 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:41:39: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
   41 |     int ans = min(dp[gy][gx][0], dp[gy][gx][1]);
      |                                       ^
main.cpp:8:23: note: 'gy' was declared here
    8 |     int h, w, sy, sx, gy, gx;
      |                       ^~
main.cpp:41:43: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
   41 |     int ans = min(dp[gy][gx][0], dp[gy][gx][1]);
      |                                           ^
main.cpp:8:27: note: 'gx' was declared here
    8 |     int h, w, sy, sx, gy, gx;
      |                           ^~
main.cpp:24:10: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
   24 |     dp[sy][sx][0] = 0;
      |          ^
main.cpp:8:15: note: 'sy' was declared here
    8 |     int h, w, sy, sx, gy, gx;
      |               ^~
main.cpp:24:14: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
   24 |     dp[sy][sx][0] = 0;
      |              ^
main.cpp:8:19: note: 'sx' was declared here
    8 |     int h, w, sy, sx, gy, gx;
      |                   ^~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w, sy, sx, gy, gx;
    cin >> h >> w;
    vector<string> A(h);
    for(int y = 0; y < h; y++){
        cin >> A[y];
        for(int x = 0; x < w; x++){
            if(A[y][x] == 'S') sy = y, sx = x;
            if(A[y][x] == 'G') gy = y, gx = x;
        }
    }
    vector<vector<pair<int,int>>> dir = {
        {{-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, 1}, {2, -1}, {-2, -1}, {-2, 1}},
        {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
    };
    queue<tuple<int,int,int>> nxt;
    vector<vector<array<int,2>>> dp(h, vector<array<int,2>>(w, array<int,2>({1 << 30, 1 << 30})));
    dp[sy][sx][0] = 0;
    nxt.emplace(sy, sx, 0);
    while(!nxt.empty()){
        int y, x, s, dy, dx, ny, nx, ns;
        tie(y, x, s) = nxt.front();
        nxt.pop();
        for(auto &&p : dir[s]){
            tie(dy, dx) = p;
            ny = y + dy;
            nx = x + dx;
            if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
            ns = s ^ (A[ny][nx] == 'R');
            if(dp[y][x][s] + 1 >= dp[ny][nx][ns]) continue;
            dp[ny][nx][ns] = dp[y][x][s] + 1;
            nxt.emplace(ny, nx, ns);
        }
    }
    int ans = min(dp[gy][gx][0], dp[gy][gx][1]);
    if(ans == (1 << 30)) ans = -1;
    cout << ans << '\n';
}
0