結果
| 問題 | No.367 ナイトの転身 | 
| コンテスト | |
| ユーザー |  777yuuki12323 | 
| 提出日時 | 2017-06-19 16:41:05 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 42 ms / 2,000 ms | 
| コード長 | 3,913 bytes | 
| コンパイル時間 | 572 ms | 
| コンパイル使用メモリ | 70,180 KB | 
| 実行使用メモリ | 12,416 KB | 
| 最終ジャッジ日時 | 2024-10-02 07:44:10 | 
| 合計ジャッジ時間 | 1,583 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
ソースコード
#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;
}
            
            
            
        