結果

問題 No.367 ナイトの転身
ユーザー tkmst201tkmst201
提出日時 2017-05-19 17:26:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,594 bytes
コンパイル時間 1,524 ms
コンパイル使用メモリ 167,440 KB
実行使用メモリ 6,060 KB
最終ジャッジ日時 2023-10-19 02:47:33
合計ジャッジ時間 2,977 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
5,552 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 24 ms
6,048 KB
testcase_11 AC 30 ms
6,060 KB
testcase_12 AC 17 ms
5,020 KB
testcase_13 AC 14 ms
5,028 KB
testcase_14 AC 16 ms
5,068 KB
testcase_15 AC 3 ms
4,916 KB
testcase_16 AC 15 ms
5,048 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 4 ms
4,348 KB
testcase_19 AC 6 ms
4,348 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 5 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> P;

const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-10;

int h, w;
string maps[500];
int dp[500][500][2];

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

int main() {
	cin >> h >> w;
	REP(i, h) cin >> maps[i];
	
	pii sp, ep;
	REP(i, h) REP(j, w) {
		if (maps[i][j] == 'S') sp = pii(i, j);
		if (maps[i][j] == 'G') ep = pii(i, j);
	}
	
	fill(dp[0][0], dp[h][0], INF);
	queue<P> que;
	
	dp[sp.first][sp.second][0] = 0;
	que.push(P(0, sp));
	
	while (!que.empty()) {
		int s = que.front().first;
		
		int x = que.front().second.second;
		int y = que.front().second.first;
		
		que.pop();
		
		REP(i, c[s]) {
			int nx = x + dx[s][i];
			int ny = y + dy[s][i];
			
			if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) continue;
			
			int ns = s ^ (maps[ny][nx] == 'R');
			
			if (chmin(dp[ny][nx][ns], dp[y][x][s] + 1))
				que.push(P(ns, pii(ny, nx)));
		}
	}
	
	int ans = INF;
	REP(i, 2) chmin(ans, dp[ep.first][ep.second][i]);
	
	if (ans == INF) ans = -1;
	cout << ans << endl;
	
	return 0;
}
0