結果

問題 No.367 ナイトの転身
ユーザー albicillaalbicilla
提出日時 2016-04-30 00:19:24
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,377 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 67,088 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 07:53:50
合計ジャッジ時間 1,627 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
6,944 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,940 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 WA -
testcase_10 AC 25 ms
6,940 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>

using namespace std;

#define INF -1
int h,w,nx,ny;
char a[500+10][500+10];
int d[500+10][500+10];
int sx,sy,gx,gy;
int kdx[8]={2,1,-1,-2,-2,-1,1,2},kdy[8]={1,2,2,1,-1,-2,-2,-1};
int bdx[4]={1,-1,-1,1},bdy[4]={1,1,-1,-1};

typedef pair<int,int> P;

bool flag=false;

void change()
{
	if(flag)flag=false;
	else flag=true;
}

int bfs()
{
	queue<P> que;
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			d[j][i]=INF;
		}
	}
	que.push(P(sx,sy));
	d[sx][sy]=0;

	while(que.size())
	{
		P p =que.front();que.pop();

		if(p.first==gx&&p.second==gy)break;

		if(a[p.first][p.second]=='R')change();

		if(flag==false)
			for(int i=0;i<8;i++)
			{
				int nx = p.first+kdx[i],ny=p.second+kdy[i];
	
			
				if(0<=nx&&nx<w&&0<=ny&&ny<h&&d[nx][ny]==INF)
				{
					que.push(P(nx,ny));
					d[nx][ny]=d[p.first][p.second]+1;
				}
		}
		else
		{
			for(int i=0;i<4;i++)
			{	
				int nx = p.first+bdx[i],ny=p.second+bdy[i];
				if(0<=nx&&nx<w&&0<=ny&&ny<h&&d[nx][ny]==INF)
				{
					que.push(P(nx,ny));
					d[nx][ny]=d[p.first][p.second]+1;
				}
			}
		}

	}
	return d[gx][gy];
}

int main()
{
	cin>>h>>w;
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			cin>>a[j][i];
			if(a[j][i]=='S')
			{
				sx=j,sy=i;
			}
			if(a[j][i]=='G')
			{
				gx=j,gy=i;
			}
		}
	}
	int ans=bfs();
	if(ans==INF)cout<<-1<<endl;
	else cout<<ans<<endl;

	

}
0