結果
| 問題 | No.367 ナイトの転身 |
| コンテスト | |
| ユーザー |
__math
|
| 提出日時 | 2016-05-18 22:23:49 |
| 言語 | C++11 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 15 ms / 2,000 ms |
| コード長 | 1,287 bytes |
| 記録 | |
| コンパイル時間 | 1,088 ms |
| コンパイル使用メモリ | 184,712 KB |
| 実行使用メモリ | 6,400 KB |
| 最終ジャッジ日時 | 2026-04-22 00:46:31 |
| 合計ジャッジ時間 | 2,396 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/algorithm:62,
from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:53,
from main.cpp:1:
In function 'const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = int]',
inlined from 'int main()' at main.cpp:54:15:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:239:15: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
239 | if (__b < __a)
| ~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:28:21: note: 'gx' was declared here
28 | int sx, sy, gx, gy;
| ^~
In function 'const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = int]',
inlined from 'int main()' at main.cpp:54:15:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:239:15: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
239 | if (__b < __a)
| ~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:28:25: note: 'gy' was declared here
28 | int sx, sy, gx, gy;
| ^~
main.cpp:34:25: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
34 | memo[sx][sy][0] = 0;
| ~~~~~~~~~~~~~~~~^~~
main.cpp:28:13: note: 'sx' was declared here
28 | int sx, sy, gx, gy;
| ^~
main.cpp:34:25: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
34 | memo[sx][sy][0] = 0;
| ~~~~~~~~~~~~~~~~^~~
main.cpp:28:17: note: 'sy' was declared here
28 | int sx, sy, gx, gy;
| ^~
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())
#define ten(n) ((int)1e##n)
using ll = long long;
int n, m;
bool in(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
int dx1[] = { 2, 2, 1, 1, -1, -1, -2, -2 };
int dy1[] = { -1, 1, -2, 2, -2, 2, -1, 1 };
int dx2[] = { 1, 1, -1, -1 };
int dy2[] = { 1, -1, 1, -1 };
char t[500][501];
int memo[500][500][2];
int main() {
cin >> n >> m;
FOR(i, n) cin >> t[i];
int sx, sy, gx, gy;
FOR(i, n) FOR(j, m) {
if (t[i][j] == 'S') sx = i, sy = j;
if (t[i][j] == 'G') gx = i, gy = j;
}
memset(memo, 0x2f, sizeof(memo));
memo[sx][sy][0] = 0;
queue<tuple<int, int, int>> q;
q.emplace(sx, sy, 0);
while (!q.empty()) {
int x, y, c; tie(x, y, c) = q.front(); q.pop();
int kmax = (c == 0) ? 8 : 4;
auto dx = (c == 0) ? dx1 : dx2;
auto dy = (c == 0) ? dy1 : dy2;
FOR(k, kmax) {
int nx = x + dx[k], ny = y + dy[k];
if (!in(nx, ny)) continue;
int nc = c;
if (t[nx][ny] == 'R') nc ^= 1;
if (memo[nx][ny][nc] > memo[x][y][c] + 1) {
memo[nx][ny][nc] = memo[x][y][c] + 1;;
q.emplace(nx, ny, nc);
}
}
}
int ans = min(memo[gx][gy][0], memo[gx][gy][1]);
if (ans > ten(5)) ans = -1;
cout << ans << endl;
return 0;
}
__math