結果

問題 No.367 ナイトの転身
ユーザー kapokapo
提出日時 2016-05-19 09:46:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,596 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 72,064 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 23:06:46
合計ジャッジ時間 1,986 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
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 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 14 ms
5,376 KB
testcase_11 AC 19 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 5 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 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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)
{
	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] = true;
					}
					else {
						q.push(make_tuple(f,n+1,y,x));
						b[y][x][0] = true;
					}
				}
			} 
		}
		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] = true;
					}
					else {
						q.push(make_tuple(f,n+1,y,x));
						b[y][x][1] = true;
					}
				}
			}
		}
	}

	cout << -1 << endl;

	return 0;
}
0