結果

問題 No.367 ナイトの転身
ユーザー kapokapo
提出日時 2016-05-19 09:32:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,647 bytes
コンパイル時間 731 ms
コンパイル使用メモリ 71,836 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 23:06:04
合計ジャッジ時間 1,570 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 WA -
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 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <tuple>
#include <queue>
#include <string>

using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define tpl tuple<bool, int, int, int>

int main(void)
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);

	int h, w, sy, sx, gy, gx, n, qy, qx, x, y;
	string s[500];
	bool b[500][500][2], f;
	int km[8][2] = { {2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1} };
	int bm[4][2] = { {1,1},{-1,1},{-1,-1},{1,-1} };
	queue<tpl> q;// is_knight node y x
	cin >> h >> w;
	rep(i,0,h) cin >> s[i];
	rep(i,0,h) rep(j,0,w) {
		if(s[i][j]=='S') { sy = i; sx = j; }
		if(s[i][j]=='G') { gy = i; gx = j; }
	}
	q.push(make_tuple(true,0,sy,sx));

	while(q.size()) {
		f = get<0>(q.front());
		n = get<1>(q.front());
		qy = get<2>(q.front());
		qx = get<3>(q.front());
		q.pop();
		if(f) {
			rep(i,0,8) {
				y = qy+km[i][0]; x = qx+km[i][1];
				if(y>=0 && y<h && x>=0 && x<w && b[y][x][0]) {
					if(s[y][x] == 'G') {
						cout << n+1 << endl;
						return 0;
					}
					if(s[y][x] == 'R') {
						q.push(make_tuple(!f,n+1,y,x));
						b[y][x][0] = false;
					}
					else {
						q.push(make_tuple(f,n+1,y,x));
						b[y][x][0] = false;
					}
				}
			} 
		}
		else if(!f) {
			rep(i,0,4) {
				y = qy+bm[i][0]; x = qx+bm[i][1];
				if(y>=0 && y<h && x>=0 && x<w && b[y][x][1]) {
					if(s[y][x] == 'G') {
						cout << n+1 << endl;
						return 0;
					}
					if(s[y][x] == 'R') {
						q.push(make_tuple(!f,n+1,y,x));
						b[y][x][1] = false;
					}
					else {
						q.push(make_tuple(f,n+1,y,x));
						b[y][x][1] = false;
					}
				}
			}
		}
	}

	cout << -1 << endl;

	return 0;
}
0