結果

問題 No.367 ナイトの転身
ユーザー purple_jwlpurple_jwl
提出日時 2016-05-06 22:52:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,008 bytes
コンパイル時間 1,444 ms
コンパイル使用メモリ 165,648 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 13:33:21
合計ジャッジ時間 2,609 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 36 ms
5,376 KB
testcase_11 AC 54 ms
5,376 KB
testcase_12 AC 9 ms
5,376 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 21 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 18 ms
5,376 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 6 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

// k: knight
const vector<int> kdx {-1, 1, -2, 2, -2, 2, -1, 1};
const vector<int> kdy {-2, -2, -1, -1, 1, 1, 2, 2};

// b: bishop
const vector<int> bdx {-1, 1, -1, 1};
const vector<int> bdy {-1, -1, 1, 1};

int H, W;
int sx, sy, gx, gy;
char board[500][500];
// k: minStep[][][0]
// b: minStep[][][1]
bool visited[500][500][2];

// k: piece == 0
// b: piece == 1
struct State {
    int x, y, step, piece;
    State() {}
    State(int _x, int _y, int _step, int _piece) :
        x(_x), y(_y), step(_step), piece(_piece) {}
};

int solve() {
    memset(visited, false, sizeof(visited));

    queue<State> Q;
    Q.push(State(sx, sy, 0, 0));
    visited[sy][sx][0] = true;

    while (!Q.empty()) {
        State stt = Q.front();
        Q.pop();

        vector<int> dx, dy;
        if (stt.piece == 0) {
            dx = kdx;
            dy = kdy;
        } else {
            dx = bdx;
            dy = bdy;
        }

        for (int i = 0; i < dx.size(); i++) {
            int x = stt.x + dx[i];
            int y = stt.y + dy[i];
            int piece = stt.piece;

            if (!(0 <= x && x < W) || !(0 <= y && y < H)) {
                continue;
            }

            if (x == gx && y == gy) {
                return stt.step + 1;
            }

            if (board[y][x] == 'R') {
                piece = 1 - piece;
            }

            if (visited[y][x][piece]) {
                continue;
            }

            visited[y][x][piece] = true;
            Q.push(State(x, y, stt.step + 1, piece));
        }
    }

    return -1;
}

int main() {
    cin >> H >> W;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> board[i][j];
            if (board[i][j] == 'S') {
                sy = i;
                sx = j;
            }
            if (board[i][j] == 'G') {
                gy = i;
                gx = j;
            }
        }
    }
    cout << solve() << endl;
    return 0;
}
0