結果

問題 No.367 ナイトの転身
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-06 09:01:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 2,179 bytes
コンパイル時間 577 ms
コンパイル使用メモリ 66,136 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-04-20 22:07:53
合計ジャッジ時間 1,666 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,504 KB
testcase_01 AC 4 ms
5,632 KB
testcase_02 AC 3 ms
5,504 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,504 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 4 ms
5,504 KB
testcase_07 AC 4 ms
5,504 KB
testcase_08 AC 4 ms
5,504 KB
testcase_09 AC 3 ms
5,504 KB
testcase_10 AC 22 ms
6,016 KB
testcase_11 AC 32 ms
6,016 KB
testcase_12 AC 8 ms
5,760 KB
testcase_13 AC 8 ms
5,760 KB
testcase_14 AC 13 ms
5,632 KB
testcase_15 AC 4 ms
5,632 KB
testcase_16 AC 13 ms
5,632 KB
testcase_17 AC 5 ms
5,504 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 5 ms
5,760 KB
testcase_20 AC 4 ms
5,504 KB
testcase_21 AC 5 ms
5,632 KB
testcase_22 AC 4 ms
5,504 KB
testcase_23 AC 3 ms
5,504 KB
testcase_24 AC 4 ms
5,504 KB
testcase_25 AC 4 ms
5,632 KB
testcase_26 AC 3 ms
5,504 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void Solve()’:
main.cpp:44:25: warning: ‘gc’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   44 |         if (s.row == gr && s.col == gc) {
      |             ~~~~~~~~~~~~^~~~~~~~~~~~~~
main.cpp:44:9: warning: ‘gr’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   44 |         if (s.row == gr && s.col == gc) {
      |         ^~
main.cpp:41:27: warning: ‘sc’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   41 |     cost[kKNIGHT][sr][sc] = 0;
      |     ~~~~~~~~~~~~~~~~~~~~~~^~~
main.cpp:41:27: warning: ‘sr’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

struct State {
    int row, col, piece, cost;
};

const int kMAX_H = 510;
const int kMAX_W = 510;

const int kKNIGHT = 0;
const int kMINI_BISHOP = 1;

string S[kMAX_H];

int H, W;
int cost[2][kMAX_H][kMAX_W];

int n_dr[8] = { 1,  1, -1, -1,  2,  2, -2, -2},
    n_dc[8] = { 2, -2,  2, -2,  1, -1,  1, -1};
int b_dr[4] = { 1,  1, -1, -1},
    b_dc[4] = { 1, -1,  1, -1};

void Solve() {
    queue<State> que;
    int sr, sc, gr, gc;
    for (int r = 0; r < H; r++) {
        for (int c = 0; c < W; c++) {
            if (S[r][c] == 'S') {
                sr = r; sc = c;
            } else if (S[r][c] == 'G') {
                gr = r; gc = c;
            }
        }
    }

    memset(cost, -1, sizeof(cost));
    que.push((State){sr, sc, kKNIGHT, 0});
    cost[kKNIGHT][sr][sc] = 0;
    while (!que.empty()) {
        State s = que.front(); que.pop();
        if (s.row == gr && s.col == gc) {
            cout << s.cost << endl;
            return;
        }

        if (s.piece == kKNIGHT) {
            for (int i = 0; i < 8; i++) {
                int nr = s.row + n_dr[i], nc = s.col + n_dc[i];
                if (0 <= nr && nr < H && 0 <= nc && nc < W) {
                    int npiece = S[nr][nc] == 'R' ? kMINI_BISHOP : kKNIGHT;
                    if (cost[npiece][nr][nc] >= 0) continue;

                    que.push((State){nr, nc, npiece, s.cost + 1});
                    cost[npiece][nr][nc] = s.cost + 1;
                }
            }
        } else {
            for (int i = 0; i < 4; i++) {
                int nr = s.row + b_dr[i], nc = s.col + b_dc[i];
                if (0 <= nr && nr < H && 0 <= nc && nc < W) {
                    int npiece = S[nr][nc] == 'R' ? kKNIGHT : kMINI_BISHOP;
                    if (cost[npiece][nr][nc] >= 0) continue;

                    que.push((State){nr, nc, npiece, s.cost + 1});
                    cost[npiece][nr][nc] = s.cost + 1;
                }
            }
        }
    }
    cout << -1 << endl;
}

int main() {
    cin >> H >> W;

    for (int i = 0; i < H; i++) {
        cin >> S[i];
    }

    Solve();

    return 0;
}
0