結果

問題 No.367 ナイトの転身
ユーザー airuaiairuai
提出日時 2016-12-03 13:55:15
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,673 bytes
コンパイル時間 453 ms
コンパイル使用メモリ 62,228 KB
実行使用メモリ 17,280 KB
最終ジャッジ日時 2024-05-05 18:07:23
合計ジャッジ時間 1,480 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
#include <list>
using namespace std;
#define N 0
#define B 1
#define MAX 250000
#define ull unsigned long long int

int q[MAX][4];
int head = 0, tail = 0;

int h, w, x, y;
int state = N;
bool*** f;
int c = 1;

void pushQ(int x, int y, int c, int s) {
	q[tail][0] = x;
	q[tail][1] = y;
	q[tail][2] = c;
	q[tail][3] = s;
	++tail;
	if (tail >= MAX) {
		tail = 0;
	}
}

void pushqueue() {
	if (state) { // state = B
		if (x - 1 >= 0) {
			if (y - 1 >= 0) {
				if (!f[y-1][x-1][state]) {
					pushQ(x - 1, y - 1, c, state);
				}
			}
			if (y + 1 < h) {
				if (!f[y+1][x-1][state]) {
					pushQ(x - 1, y + 1, c, state);
				}
			}
		}
		if (x + 1 < w) {
			if (y - 1 >= 0) {
				if (!f[y-1][x+1][state]) {
					pushQ(x + 1, y - 1, c, state);
				}
			}
			if (y + 1 < h) {
				if (!f[y+1][x+1][state]) {
					pushQ(x + 1, y + 1, c, state);
				}
			}
		}
	}
	else { // state = N
		if (x - 2 >= 0) {
			if (y - 1 >= 0) {
				if (!f[y-1][x-2][state]) {
					pushQ(x - 2, y - 1, c, state);
				}
			}
			if (y + 1 < h) {
				if (!f[y+1][x-2][state]) {
					pushQ(x - 2, y + 1, c, state);
				}
			}
		}
		if (x - 1 >= 0) {
			if (y - 2 >= 0) {
				if (!f[y-2][x-1][state]) {
					pushQ(x - 1, y - 2, c, state);
				}
			}
			if (y + 2 < h) {
				if (!f[y+2][x-1][state]) {
					pushQ(x - 1, y + 2, c, state);
				}
			}
		}
		if (x + 1 < w) {
			if (y - 2 >= 0) {
				if (!f[y-2][x+1][state]) {
					pushQ(x + 1, y - 2, c, state);
				}
			}
			if (y + 2 < h) {
				if (!f[y+2][x+1][state]) {
					pushQ(x + 1, y + 2, c, state);
				}
			}
		}
		if (x + 2 < w) {
			if (y - 1 >= 0) {
				if (!f[y-1][x+2][state]) {
					pushQ(x + 2, y - 1, c, state);
				}
			}
			if (y + 1 < h) {
				if (!f[y+1][x+2][state]) {
					pushQ(x + 2, y + 1, c, state);
				}
			}
		}
	}
}

void popq() {
	x = q[head][0];
	y = q[head][1];
	c = q[head][2];
	state = q[head][3];
	++head;
	if (head >= MAX) {
		head = 0;
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> h >> w;
	char** d = new char*[h];
	f = new bool**[h];
	for (int i = 0; i < h; ++i) {
		d[i] = new char[w];
		f[i] = new bool*[w];
		for (int j = 0; j < w; ++j) {
			cin >> d[i][j];
			f[i][j] = new bool[2];
			if (d[i][j] == 'S') {
				d[i][j] = '.';
				f[i][j][N] = true;
				f[i][j][B] = false;
				y = i;
				x = j;
			}
			else {
				f[i][j][N] = false;
				f[i][j][B] = false;
			}
		}
	}

	pushqueue();
	while (head != tail) {
		popq();
		f[y][x][state] = true;
		if (d[y][x] == 'G') {
			cout << c << endl;
			goto end;
		}
		else if (d[y][x] == 'R') {
			state ^= 1; // stateが1なら0、0なら1
		}
		++c;
		pushqueue();
	}
	cout << -1 << endl;
end:
	return 0;
}
0