結果

問題 No.367 ナイトの転身
ユーザー furafura
提出日時 2020-07-26 00:06:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,027 bytes
コンパイル時間 2,109 ms
コンパイル使用メモリ 210,512 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2023-09-10 02:18:02
合計ジャッジ時間 3,721 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,420 KB
testcase_01 AC 3 ms
5,392 KB
testcase_02 AC 3 ms
5,392 KB
testcase_03 AC 3 ms
5,436 KB
testcase_04 AC 3 ms
5,604 KB
testcase_05 AC 3 ms
5,364 KB
testcase_06 AC 3 ms
5,284 KB
testcase_07 AC 3 ms
5,300 KB
testcase_08 AC 3 ms
5,420 KB
testcase_09 AC 3 ms
5,592 KB
testcase_10 AC 27 ms
5,948 KB
testcase_11 AC 32 ms
6,016 KB
testcase_12 AC 7 ms
5,640 KB
testcase_13 AC 7 ms
5,632 KB
testcase_14 AC 13 ms
5,620 KB
testcase_15 AC 4 ms
5,404 KB
testcase_16 AC 14 ms
5,712 KB
testcase_17 AC 5 ms
5,476 KB
testcase_18 AC 5 ms
5,448 KB
testcase_19 AC 5 ms
5,496 KB
testcase_20 AC 4 ms
5,516 KB
testcase_21 AC 5 ms
5,652 KB
testcase_22 AC 3 ms
5,488 KB
testcase_23 AC 3 ms
5,500 KB
testcase_24 AC 3 ms
5,468 KB
testcase_25 AC 4 ms
5,428 KB
testcase_26 AC 3 ms
5,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main(){
	int h,w; cin>>h>>w;
	vector<string> B(h);
	rep(i,h) cin>>B[i];

	int d[500][500][2];
	memset(d,-1,sizeof d);
	queue<tuple<int,int,int>> Q;
	rep(i,h) rep(j,w) if(B[i][j]=='S') {
		d[i][j][0]=0;
		Q.emplace(i,j,0);
	}
	while(!Q.empty()){
		int i,j,t;
		tie(i,j,t)=Q.front(); Q.pop();

		if(B[i][j]=='G') return printf("%d\n",d[i][j][t]),0;

		if(t==0){
			for(int dy=-2;dy<=2;dy++) for(int dx=-2;dx<=2;dx++) if(abs(dx)+abs(dy)==3) {
				int y=i+dy,x=j+dx;
				if(0<=y && y<h && 0<=x && x<w){
					int t2=(B[y][x]=='R'?1-t:t);
					if(d[y][x][t2]==-1){
						d[y][x][t2]=d[i][j][t]+1;
						Q.emplace(y,x,t2);
					}
				}
			}
		}
		else{
			for(int dy=-1;dy<=1;dy+=2) for(int dx=-1;dx<=1;dx+=2) {
				int y=i+dy,x=j+dx;
				if(0<=y && y<h && 0<=x && x<w){
					int t2=(B[y][x]=='R'?1-t:t);
					if(d[y][x][t2]==-1){
						d[y][x][t2]=d[i][j][t]+1;
						Q.emplace(y,x,t2);
					}
				}
			}
		}
	}

	puts("-1");

	return 0;
}
0