結果

問題 No.367 ナイトの転身
ユーザー nksk38nksk38
提出日時 2017-06-19 18:05:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 2,339 bytes
コンパイル時間 725 ms
コンパイル使用メモリ 86,128 KB
実行使用メモリ 7,564 KB
最終ジャッジ日時 2024-04-10 06:06:54
合計ジャッジ時間 1,872 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<int, int> Pr;

ll INF = 1000000000;

int H, W;
int sx, sy, gx, gy;
char board[502][502];
ll d[502][502][2];
queue<pair<pair<int,int>,char>> q;

int dx_n[] = { -1,1,-2,2,-1,1,-2,2 };
int dy_n[] = { 2,-2,1,-1,-2,2,-1,1 };
int dx_m[] = { 1,-1,1,-1 };
int dy_m[] = { -1,1,1,-1 };

int main()
{
	cin >> H >> W;
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <=W; j++) {
			cin >> board[i][j];
			if (board[i][j] == 'S') {
				sx = j, sy = i;
			}
			if (board[i][j] == 'G') {
				gx = j, gy = i;
			}
		}
	}

	for (int i = 1; i <= H; i++)
		for (int j = 1; j <= W; j++)
			for (int k = 0; k < 2; k++)
				d[i][j][k] = (ll)INF;

	q.push({{sx,sy},'N'});
	d[sy][sx][1] = 0;
	while (!q.empty()) {
		int x = q.front().first.first;
		int y = q.front().first.second;
		char c = q.front().second;
		q.pop();
		if (c == 'N') {
			for (int i = 0; i < 8; i++) {
				char now = c;
				int ddx = dx_n[i] + x, ddy = dy_n[i] + y;
				if (ddx >= 1 && ddx <= W && ddy >= 1 && ddy <= H) {
					if (board[ddy][ddx] == 'R') {
						now = 'M';
					}
					
					if(now == 'N') {
						if (d[ddy][ddx][1] > d[y][x][1] + 1) {
							d[ddy][ddx][1] = d[y][x][1] + 1;
							q.push({ {ddx,ddy},now });
						}
					}
					else if (now == 'M') {
						if (d[ddy][ddx][0] > d[y][x][1] + 1) {
							d[ddy][ddx][0] = d[y][x][1] + 1;
							q.push({ { ddx,ddy },now});
						}
					}
				}
			}
		}
		else if (c == 'M') {
			for (int i = 0; i < 4; i++) {
				char now = c;
				int ddx = dx_m[i] + x, ddy = dy_m[i] + y;
				if (ddx >= 1 && ddx <= W && ddy >= 1 && ddy <= H) {
					if (board[ddy][ddx] == 'R') {
						now = 'N';
					}
					if (now == 'M') {
						if (d[ddy][ddx][0] > d[y][x][0] + 1) {
							d[ddy][ddx][0] = d[y][x][0] + 1;
							q.push({ { ddx,ddy },now });
						}
					}
					else if (now == 'N') {
						if (d[ddy][ddx][1] > d[y][x][0] + 1) {
							d[ddy][ddx][1] = d[y][x][0] + 1;
							q.push({ { ddx,ddy },now });
						}
					}
				}
			}
		}
	}
	ll ans = min(d[gy][gx][0], d[gy][gx][1]);
	if (ans != INF) {
		cout << ans << endl;
	}
	else
		cout << -1 << endl;
	return 0;
}
0