結果

問題 No.367 ナイトの転身
コンテスト
ユーザー 🍮かんプリン
提出日時 2020-05-30 18:52:09
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 2,459 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,459 ms
コンパイル使用メモリ 190,608 KB
実行使用メモリ 17,920 KB
最終ジャッジ日時 2026-05-08 06:01:40
合計ジャッジ時間 5,211 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:71:42: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
   71 |     int ans = min(dist[gx][gy][0],dist[gx][gy][1]);
      |                                          ^
main.cpp:14:15: note: 'gx' was declared here
   14 |     int sx,sy,gx,gy;
      |               ^~
main.cpp:71:46: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
   71 |     int ans = min(dist[gx][gy][0],dist[gx][gy][1]);
      |                                              ^
main.cpp:14:18: note: 'gy' was declared here
   14 |     int sx,sy,gx,gy;
      |                  ^~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/functional:55,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:55,
                 from main.cpp:7:
In constructor 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = int&; long unsigned int _Idx = 0; _Head = int]',
    inlined from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(_UHead&&, _UTail&& ...) [with _UHead = int&; _UTail = {int&, int}; <template-parameter-2-3> = void; long unsigned int _Idx = 0; _Head = int; _Tail = {int, int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/tuple:315:38,
    inlined from 'constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int&, int&, int}; bool _Valid = true; typename std::enable_if<_TCC<_Valid>::__is_implicitly_constructible<_UElements ...>(), bool>::type <anonymous> = true; _Elements = {int, int, int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/tuple:1490:54,
    inlined from 'constexpr std::tuple<typename std::__strip_reference_wrapper<typename std::decay<_Elements>::type>::__type ...> std::make_tuple(_Elements&& ...) [with _Elements = {int&, int&, int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/tuple:

ソースコード

diff #
raw source code

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.05.30 18:52:06
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int main() {
    int h,w;cin >> h >> w;
    vector<string> g(h);
    int sx,sy,gx,gy;
    for (int i = 0; i < h; i++) {
        cin >> g[i];
        for (int j = 0; j < w; j++) {
            if (g[i][j] == 'S') {
                sx = i;
                sy = j;
            }
            else if (g[i][j] == 'G') {
                gx = i;
                gy = j;
            }
        }
    }
    int k_dx[] = {-2,-1,1,2,2,1,-1,-2},k_dy[] = {1,2,2,1,-1,-2,-2,-1};
    int b_dx[] = {-1,1,1,-1}, b_dy[] = {1,1,-1,-1};
    constexpr int INF = 1e9 + 6;
    vector<vector<vector<int>>> dist(h,vector<vector<int>>(w,vector<int>(2,INF)));
    queue<tuple<int,int,int>> que;
    que.push(make_tuple(sx,sy,0));
    dist[sx][sy][0] = 0;
    while(!que.empty()) {
        auto p = que.front(); que.pop();
        int nx = get<0>(p);
        int ny = get<1>(p);
        int t = get<2>(p);
        if (t == 0) {
            for (int k = 0; k < 8; k++) {
                int x = nx + k_dx[k], y = ny + k_dy[k];
                if (x >= 0 && x < h && y >= 0 && y < w) {
                    if (g[x][y] == 'R' && dist[x][y][1] == INF) {
                        dist[x][y][1] = dist[nx][ny][0] + 1;
                        que.push(make_tuple(x,y,1));
                    }
                    else if (g[x][y] != 'R' && dist[x][y][0] == INF) {
                        dist[x][y][0] = dist[nx][ny][0] + 1;
                        que.push(make_tuple(x,y,0));
                    }
                }
            }
        }
        else {
            for (int k = 0; k < 4; k++) {
                int x = nx + b_dx[k], y = ny + b_dy[k];
                if (x >= 0 && x < h && y >= 0 && y < w) {
                    if (g[x][y] == 'R' && dist[x][y][0] == INF) {
                        dist[x][y][0] = dist[nx][ny][1] + 1;
                        que.push(make_tuple(x,y,0));
                    }
                    else if (g[x][y] != 'R' && dist[x][y][1] == INF) {
                        dist[x][y][1] = dist[nx][ny][1] + 1;
                        que.push(make_tuple(x,y,1));
                    }
                }
            }
        }
    }
    int ans = min(dist[gx][gy][0],dist[gx][gy][1]);
    if (ans == INF) {
        cout << -1 << endl;
    }
    else {
        cout << ans << endl;
    }
    return 0;
}
0