結果

問題 No.367 ナイトの転身
ユーザー E31415926E31415926
提出日時 2016-07-02 16:07:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,883 bytes
コンパイル時間 785 ms
コンパイル使用メモリ 86,852 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 05:52:59
合計ジャッジ時間 1,846 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<queue>
#include<tuple>
#include<vector>
using namespace std;

#define INF 100000000
#define ISREAD 'R'
#define ISWHITE '.'
#define ISKNIGHT	true
#define ISBISHOP	false

int knightdx[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int knightdy[8] = { -2, -1, 1, 2, 2, 1, -1, -2 };
int bishopdx[4] = { 1, 1, -1, -1 };
int bishopdy[4] = { -1, 1, 1, -1 };

int main()
{
	int h, w, i, j;
	pair<int, int> start, goal;
	queue < tuple<int, int, bool>> que;
	cin >> h >> w;
	vector<vector<int>>	d(h, vector<int>(w, 0));
	vector<vector<char>> map(h, vector<char>(w));
	for (i = 0; i < h; ++i)
		for (j = 0; j < w; ++j) {
			cin >> map[i][j];
			if (map[i][j] == 'S') {
				map[i][j] = '.';
				start = pair<int, int>(i, j);
			}
			if (map[i][j] == 'G') {
				map[i][j] = '.';
				goal = pair<int, int>(i, j);
			}
		}
	que.push(tuple<int, int, bool>(start.first, start.second, ISKNIGHT));
	while (que.size()) {
		tuple<int, int, bool> t = que.front(); que.pop();
		int x0 = get<0>(t);
		int y0 = get<1>(t);
		bool state0 = get<2>(t);
		bool state = state0;
		switch (state0) {
		case ISKNIGHT:
			for (i = 0; i < 8; ++i) {
				int x = x0 + knightdx[i];
				int y = y0 + knightdy[i];
				if (0 <= x && x < h && 0 <= y && y < w && d[x][y] == 0)
				{
					d[x][y] = d[x0][y0] + 1;
					if (map[x][y] == 'R')state = !state0;
					que.push(tuple<int, int, bool>{x, y, state});
					state = state0;
				}
			}
			break;
		case ISBISHOP:
			for (i = 0; i < 4; ++i) {
				int x = x0 + bishopdx[i];
				int y = y0 + bishopdy[i];
				if (0 <= x && x < h && 0 <= y && y < w && d[x][y] == 0)
				{
					d[x][y] = d[x0][y0] + 1;
					if (map[x][y] == 'R')state = !state0;
					que.push(tuple<int, int, bool>{x, y, state});
					state = state0;
				}
			}
			break;
		}
	}
	if (d[goal.first][goal.second])cout << d[goal.first][goal.second] << endl;
	else cout << -1 << endl;
	return 0;
}
0