結果

問題 No.367 ナイトの転身
コンテスト
ユーザー hotpepsi
提出日時 2016-05-14 00:16:32
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,718 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 645 ms
コンパイル使用メモリ 84,372 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-21 05:26:43
合計ジャッジ時間 2,124 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main(int, char**)':
main.cpp:43:42: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
   43 |                         if (x == gx && y == gy) {
      |                                        ~~^~~~~
main.cpp:13:41: note: 'gy' was declared here
   13 |         int H, W, sx = -1, sy, gx = -1, gy;
      |                                         ^~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:64,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/string:53,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/locale_classes.h:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/ios_base.h:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/ios:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/ostream.h:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/ostream:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/iostream:43,
                 from main.cpp:1:
In constructor 'constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = int; _U2 = std::pair<int, int>; typename std::enable_if<(std::_PCC<true, _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<true, _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> = true; _T1 = int; _T2 = std::pair<int, int>]',
    inlined from 'int main(int, char**)' at main.cpp:34:31:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_pair.h:902:42: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
  902 |         : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y))
      |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main(int, char**)':
main.cpp:13:

ソースコード

diff #
raw source code

#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