結果
問題 | No.367 ナイトの転身 |
ユーザー | kimiyuki |
提出日時 | 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 |
コンパイル時間 | 955 ms |
コンパイル使用メモリ | 91,452 KB |
実行使用メモリ | 6,784 KB |
最終ジャッジ日時 | 2024-10-11 00:19:41 |
合計ジャッジ時間 | 1,955 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 2 ms
5,248 KB |
testcase_10 | AC | 23 ms
6,784 KB |
testcase_11 | AC | 29 ms
6,784 KB |
testcase_12 | AC | 22 ms
5,248 KB |
testcase_13 | AC | 15 ms
5,248 KB |
testcase_14 | AC | 20 ms
5,248 KB |
testcase_15 | AC | 3 ms
5,248 KB |
testcase_16 | AC | 20 ms
5,248 KB |
testcase_17 | AC | 4 ms
5,248 KB |
testcase_18 | AC | 6 ms
5,248 KB |
testcase_19 | AC | 7 ms
5,248 KB |
testcase_20 | AC | 4 ms
5,248 KB |
testcase_21 | AC | 8 ms
5,248 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 3 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
コンパイルメッセージ
main.cpp: In function 'int main()': main.cpp:30:17: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized] 30 | dp[0][gy][gx] = 0; | ^ main.cpp:19:13: note: 'gx' was declared here 19 | int gy, gx, sy, sx; | ^~ main.cpp:30:13: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized] 30 | dp[0][gy][gx] = 0; | ^ main.cpp:19:9: note: 'gy' 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; | ^~ 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; | ^~
ソースコード
#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; }