結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
sugim48
|
| 提出日時 | 2016-04-29 23:09:09 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 126 ms / 2,000 ms |
| コード長 | 1,985 bytes |
| コンパイル時間 | 748 ms |
| コンパイル使用メモリ | 91,536 KB |
| 実行使用メモリ | 40,808 KB |
| 最終ジャッジ日時 | 2024-10-04 18:46:32 |
| 合計ジャッジ時間 | 1,935 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:52:23: warning: ‘xt’ may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | return (y * W + x) * 2 + z;
| ~~~~~~~^~~~
main.cpp:58:25: note: ‘xt’ was declared here
58 | int ys, xs, yt, xt;
| ^~
main.cpp:52:19: warning: ‘yt’ may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | return (y * W + x) * 2 + z;
| ~~^~~
main.cpp:58:21: note: ‘yt’ was declared here
58 | int ys, xs, yt, xt;
| ^~
main.cpp:52:23: warning: ‘xs’ may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | return (y * W + x) * 2 + z;
| ~~~~~~~^~~~
main.cpp:58:17: note: ‘xs’ was declared here
58 | int ys, xs, yt, xt;
| ^~
main.cpp:52:19: warning: ‘ys’ may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | return (y * W + x) * 2 + z;
| ~~^~~
main.cpp:58:13: note: ‘ys’ was declared here
58 | int ys, xs, yt, xt;
| ^~
ソースコード
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };
int INF = INT_MAX / 2;
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
vector<int> bfs(int N, vector<vector<int> >& G, int s) {
vector<int> d(N, INF); d[s] = 0;
queue<int> q; q.push(s);
while (q.size()) {
int u = q.front(); q.pop();
for (int v: G[u])
if (d[v] > d[u] + 1) {
d[v] = d[u] + 1;
q.push(v);
}
}
return d;
}
int H, W;
int f(int y, int x, int z) {
return (y * W + x) * 2 + z;
}
int main() {
cin >> H >> W;
vector<string> a(H);
int ys, xs, yt, xt;
for (int y = 0; y < H; y++) {
cin >> a[y];
for (int x = 0; x < W; x++) {
char& c = a[y][x];
if (c == 'S') ys = y, xs = x, c = '.';
if (c == 'G') yt = y, xt = x, c = '.';
}
}
int N = H * W * 2;
vector<vector<int> > G(N);
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++)
for (int z = 0; z <= 1; z++) {
int dy[2][8] = {{-1, -2, -2, -1, 1, 2, 2, 1}, {-1, -1, 1, 1, -1, -1, 1, 1}};
int dx[2][8] = {{-2, -1, 1, 2, 2, 1, -1, -2}, {-1, 1, 1, -1, -1, 1, 1, -1}};
for (int k = 0; k < 8; k++) {
int _y = y + dy[z][k], _x = x + dx[z][k];
if (_y >= 0 && _y < H && _x >= 0 && _x < W) {
int _z = z;
if (a[_y][_x] == 'R') _z ^= 1;
G[f(y, x, z)].push_back(f(_y, _x, _z));
}
}
}
vector<int> d = bfs(N, G, f(ys, xs, 0));
int ans = min(d[f(yt, xt, 0)], d[f(yt, xt, 1)]);
if (ans == INF) ans = -1;
cout << ans << endl;
}
sugim48