結果

問題 No.367 ナイトの転身
ユーザー __math__math
提出日時 2016-05-18 22:23:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,287 bytes
コンパイル時間 1,534 ms
コンパイル使用メモリ 168,460 KB
実行使用メモリ 5,760 KB
最終ジャッジ日時 2024-04-15 22:59:08
合計ジャッジ時間 2,579 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,376 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 4 ms
5,504 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 21 ms
5,632 KB
testcase_11 AC 26 ms
5,760 KB
testcase_12 AC 15 ms
5,632 KB
testcase_13 AC 13 ms
5,632 KB
testcase_14 AC 14 ms
5,504 KB
testcase_15 AC 4 ms
5,632 KB
testcase_16 AC 14 ms
5,760 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 6 ms
5,504 KB
testcase_20 AC 4 ms
5,504 KB
testcase_21 AC 6 ms
5,504 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,504 KB
testcase_25 AC 3 ms
5,504 KB
testcase_26 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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