結果

問題 No.367 ナイトの転身
ユーザー pekempeypekempey
提出日時 2016-04-29 22:47:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,153 bytes
コンパイル時間 1,265 ms
コンパイル使用メモリ 170,116 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-04-15 05:40:32
合計ジャッジ時間 2,093 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,504 KB
testcase_01 AC 3 ms
5,504 KB
testcase_02 AC 3 ms
5,504 KB
testcase_03 AC 3 ms
5,504 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,632 KB
testcase_06 AC 3 ms
5,504 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,504 KB
testcase_09 AC 2 ms
5,504 KB
testcase_10 AC 19 ms
5,888 KB
testcase_11 AC 27 ms
6,016 KB
testcase_12 AC 21 ms
5,632 KB
testcase_13 AC 13 ms
5,632 KB
testcase_14 AC 18 ms
5,632 KB
testcase_15 AC 4 ms
5,504 KB
testcase_16 AC 18 ms
5,632 KB
testcase_17 AC 5 ms
5,504 KB
testcase_18 AC 6 ms
5,376 KB
testcase_19 AC 7 ms
5,632 KB
testcase_20 AC 4 ms
5,504 KB
testcase_21 AC 8 ms
5,376 KB
testcase_22 AC 3 ms
5,504 KB
testcase_23 AC 3 ms
5,504 KB
testcase_24 AC 3 ms
5,632 KB
testcase_25 AC 3 ms
5,504 KB
testcase_26 AC 3 ms
5,504 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:49:27: warning: ‘gx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |         if (dist[gy][gx][1] == -1) dist[gy][gx][1] = 1e9;
      |             ~~~~~~~~~~~~~~^
main.cpp:49:27: warning: ‘gy’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

string g[500];
int dist[500][500][2];

vector<int> dy[2] = {
	{1, 2, 2, 1, -1, -2, -2, -1},
	{1, 1, -1, -1},
};

vector<int> dx[2] = {
	{2, 1, -1, -2, -2, -1, 1, 2},
	{1, -1, -1, 1},
};

int main() {
	int h, w;
	int sy, sx, gy, gx;
	cin >> h >> w;
	for (int i = 0; i < h; i++) {
		cin >> g[i];
		for (int j = 0; j < w; j++) {
			if (g[i][j] == 'S') sy = i, sx = j;
			if (g[i][j] == 'G') gy = i, gx = j;
		}
	}
	memset(dist, -1, sizeof(dist));
	dist[sy][sx][0] = 0;

	queue<tuple<int, int, int>> q;
	q.emplace(sy, sx, 0);
	while (!q.empty()) {
		int y, x, t;
		tie(y, x, t) = q.front(); q.pop();

		for (int k = 0; k < dy[t].size(); k++) {
			int ny = y + dy[t][k];
			int nx = x + dx[t][k];
			if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
			int nt = g[ny][nx] == 'R' ? !t : t;
			if (dist[ny][nx][nt] != -1) continue;
			dist[ny][nx][nt] = dist[y][x][t] + 1;
			q.emplace(ny, nx, nt);
		}
	}

	if (dist[gy][gx][0] == -1) dist[gy][gx][0] = 1e9;
	if (dist[gy][gx][1] == -1) dist[gy][gx][1] = 1e9;
	int ans = min(dist[gy][gx][0], dist[gy][gx][1]);
	if (ans == 1e9) ans = -1;
	cout << ans << endl;
}
0