結果

問題 No.367 ナイトの転身
ユーザー btkbtk
提出日時 2016-04-29 22:53:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,433 bytes
コンパイル時間 1,748 ms
コンパイル使用メモリ 180,268 KB
実行使用メモリ 6,216 KB
最終ジャッジ日時 2023-08-01 08:08:02
合計ジャッジ時間 3,053 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 29 ms
6,180 KB
testcase_11 AC 55 ms
6,216 KB
testcase_12 AC 27 ms
4,376 KB
testcase_13 AC 24 ms
4,376 KB
testcase_14 AC 24 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 22 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 8 ms
4,376 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 8 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:53:50: 警告: ‘gc’ may be used uninitialized [-Wmaybe-uninitialized]
   53 |         int res = min(NB[0][gr][gc], NB[1][gr][gc]);
      |                                                  ^
main.cpp:23:25: 備考: ‘gc’ はここで定義されています
   23 |         int sr, sc, gr, gc;
      |                         ^~
main.cpp:53:46: 警告: ‘gr’ may be used uninitialized [-Wmaybe-uninitialized]
   53 |         int res = min(NB[0][gr][gc], NB[1][gr][gc]);
      |                                              ^
main.cpp:23:21: 備考: ‘gr’ はここで定義されています
   23 |         int sr, sc, gr, gc;
      |                     ^~
main.cpp:32:18: 警告: ‘sc’ may be used uninitialized [-Wmaybe-uninitialized]
   32 |         que.push(vector<int>{0, sr, sc});
      |                  ^~~~~~~~~~~~~~~~~~~~~~
main.cpp:23:17: 備考: ‘sc’ はここで定義されています
   23 |         int sr, sc, gr, gc;
      |                 ^~
main.cpp:32:18: 警告: ‘sr’ may be used uninitialized [-Wmaybe-uninitialized]
   32 |         que.push(vector<int>{0, sr, sc});
      |                  ^~~~~~~~~~~~~~~~~~~~~~
main.cpp:23:13: 備考: ‘sr’ はここで定義されています
   23 |         int sr, sc, gr, gc;
      |             ^~

ソースコード

diff #

/*
* Problem link
* 
*/

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

bool inner(int v, int lb, int ub) { return lb <= v&&v <=ub; }
struct INIT{INIT(){cin.tie(0);ios_base::sync_with_stdio(false);} }init;
const int INF = (int)1e7;
const int dr[2][8] = { {1,1,-1,-1,2,-2,2,-2},{ 1,1,-1,-1,1,1,-1,-1 } };
const int dc[2][8] = { {2,-2,2,-2,1,1,-1,-1},{ 1,-1,-1,1,1,-1,-1,1 } };
int main() {
#ifdef INPUT_FROM_FILE
	ifstream cin("sample.in");
	ofstream cout("sample.out");
#endif
	int R, C;
	cin >> R >> C;
	vector<string> S(R);
	for (auto& it : S)cin >> it;
	int sr, sc, gr, gc;
	for (int r = 0; r < R; r++)
		for (int c = 0; c < C; c++)
			if (S[r][c] == 'S')sr = r, sc = c;
		else if (S[r][c] == 'G')gr = r, gc = c;

	vector<vector<vector<int>>> NB(2,vector<vector<int>>(R, vector<int>(C, INF)));
	NB[0][sr][sc] = 0;
	queue<vector<int>> que;
	que.push(vector<int>{0, sr, sc});



	while (que.empty() == false) {
		auto v = que.front(); que.pop();
		int type = v[0];
		int r = v[1];
		int c = v[2];
		for (int i = 0; i < 8; i++) {
			int nr = r + dr[type][i];
			int nc = c + dc[type][i];
			if (inner(nr, 0, R - 1) && inner(nc, 0, C - 1)) {
				int nt = type ^ (S[nr][nc] == 'R');
				if (NB[nt][nr][nc]> NB[type][r][c] + 1) {
					NB[nt][nr][nc] = NB[type][r][c] + 1;
					que.push(vector<int>{nt, nr, nc});
				}
			}
		}
	}
	int res = min(NB[0][gr][gc], NB[1][gr][gc]);
	if (res == INF)res = -1;
	cout << res << endl;
	return 0;
}
0