結果

問題 No.367 ナイトの転身
ユーザー sugim48sugim48
提出日時 2016-04-29 22:38:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 1,795 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 88,232 KB
実行使用メモリ 17,348 KB
最終ジャッジ日時 2023-07-27 18:43:54
合計ジャッジ時間 2,757 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 52 ms
17,348 KB
testcase_11 AC 65 ms
17,344 KB
testcase_12 AC 37 ms
8,776 KB
testcase_13 AC 27 ms
8,820 KB
testcase_14 AC 32 ms
8,668 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 32 ms
7,964 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 9 ms
4,976 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 11 ms
4,816 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:73:38: warning: ‘tx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  int ans = min(d[ty][tx][0], d[ty][tx][1]);
                                      ^
main.cpp:73:34: warning: ‘ty’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  int ans = min(d[ty][tx][0], d[ty][tx][1]);
                                  ^
main.cpp:55:10: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  d[sy][sx][0] = 0;
          ^
main.cpp:55:6: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  d[sy][sx][0] = 0;
      ^

ソースコード

diff #

#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; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

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}};

int H, W;

int f(int y, int x, int t) {
	return (y * W + x) * 2 + t;
}

int main() {
	cin >> H >> W;
	vector<string> a(H);
	int sy, sx, ty, tx;
	for (int y = 0; y < H; y++) {
		cin >> a[y];
		for (int x = 0; x < W; x++) {
			if (a[y][x] == 'S') sy = y, sx = x, a[y][x] = '.';
			if (a[y][x] == 'G') ty = y, tx = x, a[y][x] = '.';
		}
	}
	vector<vector<vector<int> > > d(H, vector<vector<int> >(W, vector<int>(2, INT_MAX)));
	d[sy][sx][0] = 0;
	queue<int> q;
	q.push(f(sy, sx, 0));
	while (!q.empty()) {
		int z = q.front(); q.pop();
		int y = z / 2 / W, x = z / 2 % W, t = z % 2;
		for (int k = 0; k < 8; k++) {
			int _y = y + dy[t][k], _x = x + dx[t][k];
			if (_y >= 0 && _y < H && _x >= 0 && _x < W) {
				int _t = t;
				if (a[_y][_x] == 'R') _t ^= 1;
				if (d[_y][_x][_t] > d[y][x][t] + 1) {
					d[_y][_x][_t] = d[y][x][t] + 1;
					q.push(f(_y, _x, _t));
				}
			}
		}
	}
	int ans = min(d[ty][tx][0], d[ty][tx][1]);
	if (ans == INT_MAX) ans = -1;
	cout << ans << endl;
}
0