結果

問題 No.367 ナイトの転身
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-05-30 18:52:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 2,459 bytes
コンパイル時間 1,404 ms
コンパイル使用メモリ 160,308 KB
実行使用メモリ 17,592 KB
最終ジャッジ日時 2023-08-07 17:50:53
合計ジャッジ時間 3,018 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 49 ms
17,592 KB
testcase_11 AC 51 ms
17,536 KB
testcase_12 AC 31 ms
8,820 KB
testcase_13 AC 22 ms
8,824 KB
testcase_14 AC 26 ms
8,652 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 25 ms
8,048 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 6 ms
4,376 KB
testcase_19 AC 8 ms
4,852 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 8 ms
4,808 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:34:16: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     dist[sx][sy][0] = 0;
                ^
main.cpp:34:12: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     dist[sx][sy][0] = 0;
            ^
main.cpp:71:46: warning: ‘gy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     int ans = min(dist[gx][gy][0],dist[gx][gy][1]);
                                              ^
main.cpp:71:42: warning: ‘gx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     int ans = min(dist[gx][gy][0],dist[gx][gy][1]);
                                          ^

ソースコード

diff #

/**
 *   @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