結果

問題 No.367 ナイトの転身
ユーザー hotpepsihotpepsi
提出日時 2016-05-14 00:16:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,718 bytes
コンパイル時間 1,146 ms
コンパイル使用メモリ 68,796 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 17:50:23
合計ジャッジ時間 2,160 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 4 ms
6,944 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 21 ms
6,944 KB
testcase_11 AC 26 ms
6,944 KB
testcase_12 AC 7 ms
6,944 KB
testcase_13 AC 8 ms
6,940 KB
testcase_14 AC 12 ms
6,940 KB
testcase_15 AC 4 ms
6,940 KB
testcase_16 AC 12 ms
6,944 KB
testcase_17 AC 5 ms
6,944 KB
testcase_18 AC 5 ms
6,944 KB
testcase_19 AC 5 ms
6,940 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 5 ms
6,940 KB
testcase_22 AC 4 ms
6,940 KB
testcase_23 AC 3 ms
6,948 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main(int, char**)’:
main.cpp:35:24: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   35 |         vis[sy][sx][0] = 1;
      |         ~~~~~~~~~~~~~~~^~~
main.cpp:43:42: warning: ‘gy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   43 |                         if (x == gx && y == gy) {
      |                                        ~~^~~~~

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <cstdio>
#include <vector>

using namespace std;

typedef pair<int, int> II;
typedef pair<int, II> III;
typedef vector<III> IIIVec;

int main(int argc, char *argv[]) {
	int H, W, sx = -1, sy, gx = -1, gy;
	int vis[500][500][2] = {};
	string s[5000];
	cin >> H >> W;
	for (int i = 0; i < H; ++i) {
		cin >> s[i];
		if (sx < 0) {
			sx = s[i].find('S');
			if (sx >= 0) {
				sy = i;
			}
		}
		if (gx < 0) {
			gx = s[i].find('G');
			if (gx >= 0) {
				gy = i;
			}
		}
	}

	IIIVec q;
	q.push_back(III(0, II(sy, sx)));
	vis[sy][sx][0] = 1;
	int ans = -1;
	while (q.size()) {
		++ans;
		IIIVec n;
		for (int i = 0; i != q.size(); ++i) {
			int t = q[i].first;
			int x = q[i].second.second, y = q[i].second.first;
			if (x == gx && y == gy) {
				cout << ans << endl;
				return 0;
			}
			if (t == 0) {
				const int dx[] = { -2,-1,1,2,2,1,-1,-2 };
				const int dy[] = { -1,-2,-2,-1,1,2,2,1 };
				for (int d = 0; d < 8; ++d) {
					int nx = x + dx[d], ny = y + dy[d];
					if (nx >= 0 && nx < W && ny >= 0 && ny < H) {
						int nt = t;
						if (s[ny][nx] == 'R') {
							nt ^= 1;
						}
						if (!vis[ny][nx][nt]) {
							vis[ny][nx][nt] = 1;
							n.push_back(III(nt, II(ny, nx)));
						}
					}
				}
			} else {
				const int dx[] = { -1,1,1,-1 };
				const int dy[] = { -1,-1,1,1 };
				for (int d = 0; d < 4; ++d) {
					int nx = x + dx[d], ny = y + dy[d];
					if (nx >= 0 && nx < W && ny >= 0 && ny < H) {
						int nt = t;
						if (s[ny][nx] == 'R') {
							nt ^= 1;
						}
						if (!vis[ny][nx][nt]) {
							vis[ny][nx][nt] = 1;
							n.push_back(III(nt, II(ny, nx)));
						}
					}
				}
			}
		}
		q = n;
	}
	cout << -1 << endl;
	return 0;
}
0