結果

問題 No.367 ナイトの転身
ユーザー yasu_likes_catsyasu_likes_cats
提出日時 2016-07-08 23:36:14
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,980 bytes
コンパイル時間 587 ms
コンパイル使用メモリ 70,940 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-21 08:31:14
合計ジャッジ時間 2,063 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 WA -
testcase_10 AC 112 ms
5,376 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 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <string>
#include <map>

const int INF = 10000000;

using namespace std;

typedef pair<int,int> P;
int ary[550][550];

int nxmove[8] = {1,-1,1,-1,2,-2,2,-2};
int nymove[8] = {2,2,-2,-2,1,1,-1,-1};

int bxmove[4] = {1,-1,1,-1};
int bymove[4] = {1,1,-1,-1};

int sx ,sy,gx,gy;

int h,w;
string str[1010];

bool koma = 0;

int bfs()
{
	queue<P> que;

	for(int i=0; i<h; i++)
		for(int j=0; j<w; j++)

			ary[i][j] = INF;

			que.push(P(sx,sy));
			ary[sy][sx] = 0;

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

				int x = p.first;
				int y = p.second;

				if(x == gx && y == gy)
					break;

					if(str[y][x] == 'R'){
						if(koma == 1)
						{
							for(int i=0; i<8; i++)
						{

							int mx = x + nxmove[i];
							int my = y + nymove[i];

							if(mx < 0 || my < 0 || mx > w || my > h)
							continue;

							if(INF == ary[my][mx])
							que.push(P(mx,my));
							ary[my][mx] = ary[y][x] + 1;
							koma = 0;
							}
						}
					for(int i = 0; i < 4; i++)
					{
							int mx = x + bxmove[i];
							int my = y + bymove[i];

							if(mx < 0 || my < 0 || mx > w || my > h)
							continue;

						if(INF == ary[my][mx])
							que.push(P(mx,my));
							ary[my][mx] = ary[y][x] + 1;
							koma = 1;

					}
				}

				for(int i=0; i<8; i++)
				{

						int mx = x + nxmove[i];
						int my = y + nymove[i];

					if(mx < 0 || my < 0 || mx > w || my > h)
						continue;

					if(INF == ary[my][mx])
						que.push(P(mx,my));
						ary[my][mx] = ary[y][x] + 1;
						koma = 0;
				}

		}
		return ary[gy][gx];
}

int main()
{
	cin >> h >> w;

	for(int i = 0; i < h; i++)
	{
		cin >> str[i];

	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++)
		{
			if(str[i][j] == 'S')
			{
				sx = j;
				sy = i;
			}
				else if(str[i][j] == 'G')
				{
					gx = j;
					gy = i;
				}
			}
		}
	}

	int ans = bfs();

	if(ans != INF)
	cout << ans << endl;

	else
		cout << -1 << endl;

	return 0;
}
0