結果

問題 No.367 ナイトの転身
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2016-04-30 10:03:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 647 ms / 2,000 ms
コード長 1,280 bytes
コンパイル時間 1,601 ms
コンパイル使用メモリ 175,244 KB
実行使用メモリ 35,456 KB
最終ジャッジ日時 2024-04-15 08:03:24
合計ジャッジ時間 3,839 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 355 ms
19,456 KB
testcase_11 AC 647 ms
35,456 KB
testcase_12 AC 31 ms
6,940 KB
testcase_13 AC 41 ms
6,944 KB
testcase_14 AC 136 ms
11,008 KB
testcase_15 AC 4 ms
6,944 KB
testcase_16 AC 133 ms
10,880 KB
testcase_17 AC 18 ms
6,940 KB
testcase_18 AC 26 ms
6,940 KB
testcase_19 AC 22 ms
6,940 KB
testcase_20 AC 5 ms
6,940 KB
testcase_21 AC 24 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef tuple<int, int, int> tiii;
typedef pair<tiii, int> pti;

int dx[2][8] = { { -2, -1, 1, 2, -2, -1, 1, 2 } ,{ -1, 1, -1, 1, 0, 0, 0, 0 } };
int dy[2][8] = { { -1, -2, -2, -1, 1, 2, 2, 1 } ,{ -1, -1, 1,1, 0, 0, 0, 0 } };
int dnum[2] = { 8, 4 };

int main()
{
	int H, W; cin >> H >> W;
	vector<string> s(H); rep(i, 0, H) cin >> s[i];

	int sx, sy;
	rep(y, 0, H) rep(x, 0, W) if (s[y][x] == 'S')
	{
		sx = x;
		sy = y;
	}

	set<tiii> done;
	queue<pti> que;

	que.push(pti(tiii(sx, sy, 0), 0));
	while (!que.empty())
	{
		pti p = que.front(); que.pop();

		if (done.find(p.first) != done.end()) continue;
		done.insert(p.first);

		int x = get<0>(p.first);
		int y = get<1>(p.first);
		int type = get<2>(p.first);
		int cost = p.second;

		//printf("(%d,%d,%d,%d)\n", x, y, type, cost);

		if (s[y][x] == 'G')
		{
			cout << cost << endl;
			return 0;
		}

		rep(i, 0, dnum[type])
		{
			int xx = x + dx[type][i];
			int yy = y + dy[type][i];
			
			if (xx < 0 || W <= xx) continue;
			if (yy < 0 || H <= yy) continue;

			int ttype;
			if (s[yy][xx] == 'R')
				ttype = (type + 1) % 2;
			else
				ttype = type;

			que.push(pti(tiii(xx, yy, ttype),cost + 1));
		}
	}
	cout << -1 << endl;
}
0