結果

問題 No.367 ナイトの転身
ユーザー 777yuuki12323777yuuki12323
提出日時 2017-06-19 16:41:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 3,913 bytes
コンパイル時間 544 ms
コンパイル使用メモリ 70,180 KB
実行使用メモリ 12,532 KB
最終ジャッジ日時 2024-04-10 06:06:18
合計ジャッジ時間 1,661 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,844 KB
testcase_01 AC 3 ms
8,728 KB
testcase_02 AC 3 ms
8,708 KB
testcase_03 AC 2 ms
9,760 KB
testcase_04 AC 3 ms
8,384 KB
testcase_05 AC 2 ms
9,304 KB
testcase_06 AC 3 ms
9,628 KB
testcase_07 AC 3 ms
8,128 KB
testcase_08 AC 3 ms
8,188 KB
testcase_09 AC 3 ms
8,640 KB
testcase_10 AC 22 ms
12,272 KB
testcase_11 AC 39 ms
12,532 KB
testcase_12 AC 9 ms
9,404 KB
testcase_13 AC 8 ms
9,356 KB
testcase_14 AC 19 ms
11,840 KB
testcase_15 AC 4 ms
9,040 KB
testcase_16 AC 20 ms
11,896 KB
testcase_17 AC 6 ms
8,616 KB
testcase_18 AC 6 ms
9,256 KB
testcase_19 AC 6 ms
8,324 KB
testcase_20 AC 4 ms
8,764 KB
testcase_21 AC 6 ms
8,920 KB
testcase_22 AC 2 ms
8,688 KB
testcase_23 AC 3 ms
8,992 KB
testcase_24 AC 4 ms
9,212 KB
testcase_25 AC 3 ms
8,528 KB
testcase_26 AC 3 ms
8,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <bitset>


typedef long long ll;
#define fi first
#define se second
const ll mod = 1000000007;
//              123456789

using namespace std;

///////////////////////////////////////////////
//
//
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////

int H;
int W;

string S[512];

int nx[8] = { -2, -1, 1, 2, 2, 1, -1, -2 };
int ny[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int bx[4] = { 1, 1, -1, -1 };
int by[4] = { 1, -1, 1, -1 };

int ndist[512][512];
int bdist[512][512];
int ch[512][512];
int flag[512][512];
pair<int, int>Q[512345];
bool q[512345];//ナイトのときは0

int sx, sy;
int gx, gy;

int main(){
	
	cin>>H>>W;
	
	int i;
	int j;
	int x;
	int y;
	int Pop = 0;
	int Push = 0;
	
	fill( bdist[0], bdist[512], 1123456789 );
	fill( ndist[0], ndist[512], 1123456789 );
	fill( ch[0], ch[512], 0 );//変わるところは1
	fill( flag[0], flag[512], 3 );//ナイトで通ったら-2、
	
	
	for( i = 0; i < H; i++ ){
		cin>>S[i];
	}
	
	for( i = 0; i < H; i++ ){
		for( j = 0; j < W; j++ ){
			if( S[i][j] == 'S' ){
				sx = j;
				sy = i;
				ndist[j][i] = 0;
				Q[Push].fi = j;
				Q[Push].se = i;
				q[Push] = 0;
				Push++;
			}
			if( S[i][j] == 'G' ){
				gx = j;
				gy = i;
			}
			if( S[i][j] == 'R' ){
				ch[j][i] = 1;
			}
		}
	}
	
	while( Pop != Push ){
		
		
		if( Q[Pop].fi == gx && Q[Pop].se == gy ) break;
		if( q[Pop] ){//現在ビショップのとき
			if( flag[Q[Pop].fi][Q[Pop].se]%2  ){
				for( i = 0; i < 4; i++ ){
					x = Q[Pop].fi + bx[i];
					y = Q[Pop].se + by[i];
					if( 0 <= x && x < W && 0 <= y && y < H ){//移動先が範囲内かどうか
						if( ch[x][y] == 0 && (flag[x][y]%2) == 1 ){//移動後ビショップ
							
							bdist[x][y] = min( bdist[x][y], bdist[Q[Pop].fi][Q[Pop].se]+1);
							
							Q[Push].fi = x;
							Q[Push].se = y;
							q[Push] = 1;
							Push++;
						}
						else if( ch[x][y] && flag[x][y] > 1 ){//移動後ナイト
						
							ndist[x][y] = min( ndist[x][y], bdist[Q[Pop].fi][Q[Pop].se]+1);
							
							Q[Push].fi = x;
							Q[Push].se = y;
							q[Push] = 0;
							Push++;
						}
					}
				}
				flag[Q[Pop].fi][Q[Pop].se]--;
			}
		}
		else{//現在ナイトのとき
			if( flag[Q[Pop].fi][Q[Pop].se] > 1 ){ 
				for( i = 0; i < 8; i++ ){
					x = Q[Pop].fi + nx[i];
					y = Q[Pop].se + ny[i];
					if( 0 <= x && x < W && 0 <= y && y < H ){//移動先が範囲内かどうか
						if( ch[x][y] && (flag[x][y]%2) == 1 ){//移動後ビショップ
							
							bdist[x][y] = min( bdist[x][y], ndist[Q[Pop].fi][Q[Pop].se]+1);
							
							Q[Push].fi = x;
							Q[Push].se = y;
							q[Push] = 1;
							Push++;
						}
						else if( ch[x][y] == 0 && flag[x][y] > 1 ){//移動後ナイト
							
							ndist[x][y] = min( ndist[x][y], ndist[Q[Pop].fi][Q[Pop].se]+1);
							
							Q[Push].fi = x;
							Q[Push].se = y;
							q[Push] = 0;
							Push++;
						}
					}
				}
				flag[Q[Pop].fi][Q[Pop].se] -= 2;	
			}	
		}
		Pop++;
		Pop%=500000;
		Push%=500000;
	}
	
	int ans;
	
	ans = min( ndist[gx][gy], bdist[gx][gy] );
	
	/*
	puts("ナイトで着地");
	for( i = 0; i < H; i++ ){
		for( j = 0; j < W; j++ ){
			if( j ) printf(" ");
			printf("%d", ndist[i][j] );
		}
		puts("");
	}
	puts("ビショップで着地");
	for( i = 0; i < H; i++ ){
		for( j = 0; j < W; j++ ){
			if( j ) printf(" ");
			printf("%d", bdist[i][j] );
		}
		puts("");
	}
	puts("flag");
	for( i = 0; i < H; i++ ){
		for( j = 0; j < W; j++ ){
			if( j ) printf(" ");
			printf("%d", flag[i][j] );
		}
		puts("");
	}
	
	for( i = 0; i < Pop; i++ ){
		printf("(%d, %d)\n", Q[i].fi, Q[i].se );
	}
	
	cout<<Push<<" "<<Pop<<endl;
	*/
	if( ans != 1123456789 ){
		cout<<ans<<endl;
	}
	else{ puts("-1");}
	
	return 0;
}
0