結果

問題 No.367 ナイトの転身
ユーザー 777yuuki12323777yuuki12323
提出日時 2017-06-19 16:38:22
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 3,875 bytes
コンパイル時間 691 ms
コンパイル使用メモリ 69,924 KB
実行使用メモリ 12,292 KB
最終ジャッジ日時 2024-04-10 06:06:16
合計ジャッジ時間 2,354 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,776 KB
testcase_01 AC 3 ms
8,972 KB
testcase_02 AC 3 ms
8,400 KB
testcase_03 AC 3 ms
8,740 KB
testcase_04 AC 4 ms
9,212 KB
testcase_05 AC 3 ms
9,128 KB
testcase_06 AC 3 ms
9,604 KB
testcase_07 AC 4 ms
7,876 KB
testcase_08 AC 3 ms
9,008 KB
testcase_09 AC 3 ms
8,400 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 10 ms
9,740 KB
testcase_13 AC 8 ms
9,744 KB
testcase_14 AC 19 ms
12,088 KB
testcase_15 AC 4 ms
8,120 KB
testcase_16 AC 20 ms
11,956 KB
testcase_17 AC 5 ms
8,436 KB
testcase_18 AC 7 ms
8,868 KB
testcase_19 AC 6 ms
9,124 KB
testcase_20 AC 4 ms
8,864 KB
testcase_21 AC 7 ms
9,188 KB
testcase_22 AC 4 ms
8,384 KB
testcase_23 AC 4 ms
9,236 KB
testcase_24 AC 4 ms
8,392 KB
testcase_25 AC 3 ms
8,872 KB
testcase_26 AC 4 ms
8,056 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++;
	}
	
	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