結果

問題 No.367 ナイトの転身
ユーザー kimiyukikimiyuki
提出日時 2016-04-30 00:23:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,827 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 90,652 KB
実行使用メモリ 6,912 KB
最終ジャッジ日時 2024-04-19 07:40:37
合計ジャッジ時間 2,169 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 22 ms
6,784 KB
testcase_11 AC 29 ms
6,912 KB
testcase_12 AC 23 ms
5,376 KB
testcase_13 AC 15 ms
5,376 KB
testcase_14 AC 20 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 19 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 7 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 7 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:48:42: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
   48 |     int ans = min(dp[0][sy][sx], dp[1][sy][sx]);
      |                                          ^
main.cpp:19:17: note: 'sy' was declared here
   19 |     int gy, gx, sy, sx;
      |                 ^~
main.cpp:48:46: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
   48 |     int ans = min(dp[0][sy][sx], dp[1][sy][sx]);
      |                                              ^
main.cpp:19:21: note: 'sx' was declared here
   19 |     int gy, gx, sy, sx;
      |                     ^~
In file included from main.cpp:4:
In constructor 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = int&; long unsigned int _Idx = 1; _Head = int]',
    inlined from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(_UHead&&, _UTail&& ...) [with _UHead = int&; _UTail = {int&}; <template-parameter-2-3> = void; long unsigned int _Idx = 1; _Head = int; _Tail = {int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/tuple:293:38,
    inlined from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(_UHead&&, _UTail&& ...) [with _UHead = bool; _UTail = {int&, int&}; <template-parameter-2-3> = void; long unsigned int _Idx = 0; _Head = bool; _Tail = {int, int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/tuple:293:38,
    inlined from 'constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {bool, int&, int&}; bool _Valid = true; typename std::enable_if<_TCC<_Valid>::__is_implicitly_constructible<_UElements ...>(), bool>::type <anonymous> = true; _Elements = {bool, int, int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/tuple:891:54,
    inlined from 'constexpr std::tuple<typename std::__strip_reference_wrapper<typename std::decay<_Elements>::type>::__type ...> std::make_tuple(_Elements&& ...) [with _Elements

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
using namespace std;
const vector<int> knight_move_y { 1, 2, 2, 1, -1, -2, -2, -1 };
const vector<int> knight_move_x { 2, 1, -1, -2, -2, -1, 1, 2 };
const vector<int> bishop_move_y { 1, 1, -1, -1 };
const vector<int> bishop_move_x { 1, -1, -1, 1 };
const int inf = 1e9+7;
int main() {
    // input
    int h, w; cin >> h >> w;
    vector<string> s(h); repeat (y,h) cin >> s[y];
    // prepare
    auto on_field = [&](int y, int x) { return 0 <= y and y < h and 0 <= x and x < w; };
    int gy, gx, sy, sx;
    repeat (y,h) repeat (x,w) {
        switch (s[y][x]) {
            case 'S': sy = y; sx = x; break;
            case 'G': gy = y; gx = x; break;
        }
    }
    // bfs
    vector<vector<vector<int> > > dp(2, vector<vector<int> >(h, vector<int>(w, inf)));
    queue<tuple<bool,int,int> > que;
    que.push(make_tuple(false, gy, gx));
    dp[0][gy][gx] = 0;
    while (not que.empty()) {
        bool bishop; int y, x; tie(bishop, y, x) = que.front(); que.pop();
        vector<int> const & dy = bishop ? bishop_move_y : knight_move_y;
        vector<int> const & dx = bishop ? bishop_move_x : knight_move_x;
        int len = dy.size();
        repeat (i,len) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (not on_field(ny, nx)) continue;
            bool nbishop = s[ny][nx] == 'R' ? not bishop : bishop;
            if (dp[nbishop][ny][nx] == inf) {
                dp[nbishop][ny][nx] = dp[bishop][y][x] + 1;
                que.push(make_tuple(nbishop, ny, nx));
            }
        }
    }
    // output
    int ans = min(dp[0][sy][sx], dp[1][sy][sx]);
    if (ans == inf) ans = -1;
    cout << ans << endl;
    return 0;
}
0