結果
| 問題 | No.367 ナイトの転身 | 
| コンテスト | |
| ユーザー |  hotpepsi | 
| 提出日時 | 2016-05-14 00:16:32 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 26 ms / 2,000 ms | 
| コード長 | 1,718 bytes | 
| コンパイル時間 | 631 ms | 
| コンパイル使用メモリ | 68,640 KB | 
| 実行使用メモリ | 6,820 KB | 
| 最終ジャッジ日時 | 2024-10-05 19:15:13 | 
| 合計ジャッジ時間 | 1,621 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
コンパイルメッセージ
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) {
      |                                        ~~^~~~~
            
            ソースコード
#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;
}
            
            
            
        