結果

問題 No.367 ナイトの転身
ユーザー finefine
提出日時 2016-04-30 01:11:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,855 bytes
コンパイル時間 1,649 ms
コンパイル使用メモリ 174,860 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-11 00:21:52
合計ジャッジ時間 2,629 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 2 ms
6,816 KB
testcase_10 AC 30 ms
6,820 KB
testcase_11 AC 46 ms
6,820 KB
testcase_12 AC 5 ms
6,820 KB
testcase_13 AC 6 ms
6,816 KB
testcase_14 AC 14 ms
6,816 KB
testcase_15 AC 2 ms
6,820 KB
testcase_16 AC 13 ms
6,816 KB
testcase_17 AC 4 ms
6,820 KB
testcase_18 AC 4 ms
6,820 KB
testcase_19 AC 5 ms
6,820 KB
testcase_20 AC 2 ms
6,820 KB
testcase_21 AC 4 ms
6,816 KB
testcase_22 AC 2 ms
6,820 KB
testcase_23 AC 1 ms
6,816 KB
testcase_24 AC 2 ms
6,816 KB
testcase_25 AC 2 ms
6,816 KB
testcase_26 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:48:23: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
   48 |         if (m.x == gx && m.y == gy) {
      |             ~~~~~~~~~~^~~~~~~~~~~~
main.cpp:29:21: note: 'gy' was declared here
   29 |     int sx, sy, gx, gy;
      |                     ^~
main.cpp:48:9: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
   48 |         if (m.x == gx && m.y == gy) {
      |         ^~
main.cpp:29:17: note: 'gx' was declared here
   29 |     int sx, sy, gx, gy;
      |                 ^~
In constructor 'Move::Move(int, int, int, int)',
    inlined from 'int main()' at main.cpp:43:12:
main.cpp:7:57: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
    7 |     Move(int x, int y, int count, int isknight) : x(x), y(y), count(count), isknight(isknight) {}
      |                                                         ^~~~
main.cpp: In function 'int main()':
main.cpp:29:13: note: 'sy' was declared here
   29 |     int sx, sy, gx, gy;
      |             ^~
In constructor 'Move::Move(int, int, int, int)',
    inlined from 'int main()' at main.cpp:43:12:
main.cpp:7:51: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
    7 |     Move(int x, int y, int count, int isknight) : x(x), y(y), count(count), isknight(isknight) {}
      |                                                   ^~~~
main.cpp: In function 'int main()':
main.cpp:29:9: note: 'sx' was declared here
   29 |     int sx, sy, gx, gy;
      |         ^~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

struct Move {
    int x, y, count, isknight;
    Move(int x, int y, int count, int isknight) : x(x), y(y), count(count), isknight(isknight) {}
};

int h, w;

bool judge_inside(int x, int y) {
    return x >= 0 && x < w && y >= 0 && y < h;
}

int dx[] = {2, 2, 1, 1, -1, -1, -2, -2};
int dy[] = {1, -1, 2, -2, 2, -2, 1, -1};

int dx2[] = {1, -1, -1, 1};
int dy2[] = {1, 1, -1, -1};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> h >> w;
    bool memo[500][500][2];
    vector<string> str(h);
    for (int i = 0; i < h; i++) cin >> str[i];
    int sx, sy, gx, gy;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (str[i][j] == 'S') {
                sy = i; 
                sx = j;
            } else if (str[i][j] == 'G') {
                gy = i;
                gx = j;
            }
            for (int k = 0; k < 2; k++) memo[i][j][k] = false;
        }
    }
    queue<Move> q;
    q.push(Move(sx, sy, 0, 1));
    int ans = -1;
    while(!q.empty()) {
        Move m = q.front();
        q.pop();
        if (m.x == gx && m.y == gy) {
            ans = m.count;
            break;
        }
        if (memo[m.y][m.x][m.isknight]) continue;
        memo[m.y][m.x][m.isknight] = true;
        if (str[m.y][m.x] == 'R') m.isknight ^= 1;
        if (m.isknight) {
            for (int i = 0; i < 8; i++) {
                if (!judge_inside(m.x + dx[i], m.y + dy[i])) continue;
                q.push(Move(m.x + dx[i], m.y + dy[i], m.count + 1, 1));
            }
        } else {
            for (int i = 0; i < 4; i++) {
                if (!judge_inside(m.x + dx2[i], m.y + dy2[i])) continue;
                q.push(Move(m.x + dx2[i], m.y + dy2[i], m.count + 1, 0));
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
0