結果

問題 No.367 ナイトの転身
ユーザー masamasa
提出日時 2016-04-29 23:26:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 2,246 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 86,680 KB
実行使用メモリ 5,792 KB
最終ジャッジ日時 2023-07-27 19:07:13
合計ジャッジ時間 2,160 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 17 ms
5,776 KB
testcase_11 AC 22 ms
5,792 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 6 ms
4,512 KB
testcase_14 AC 9 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 9 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:32: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  que.push(make_tuple(sx * 1000 + sy, 0, 'k'));
                      ~~~~~~~~~~^~~~
main.cpp:58:25: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  que.push(make_tuple(sx * 1000 + sy, 0, 'k'));
                      ~~~^~~~~~

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
#include <tuple>

using namespace std;

typedef tuple<int, int, char> TUP;

const int INF = 1e9;

void show(vector<vector<int>> &vv) {
	int h = vv.size();
	int w = vv[0].size();

	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			printf("%d ", vv[i][j] == INF ? 0 : vv[i][j]);
		}
		cout << endl;
	}
	cout << endl;
}

int main() {
	int h, w;

	cin >> h >> w;
	vector<string> board;

	string str;
	int sx, sy;
	for (int i = 0; i < h; i++) {
		cin >> str;
		auto it = find(str.begin(), str.end(), 'S');
		if (it != str.end()) {
			sy = i;
			sx = it - str.begin();
		}
		board.emplace_back(str);
	}

	vector<vector<int>> knight, bishop;
	knight.assign(board.size(), vector<int>(board[0].size(), INF));
	knight[sy][sx] = 0;
	bishop = knight;

	int k_dx[8] = {2, 1, -1, -2, -2, -1,  1, 2};
	int k_dy[8] = {1, 2,  2,  1, -1, -2, -2, -1};
	int b_dx[4] = {1, -1, -1,  1};
	int b_dy[4] = {1,  1, -1, -1};

	queue<TUP> que;
	que.push(make_tuple(sx * 1000 + sy, 0, 'k'));

	while (!que.empty()) {
		auto t = que.front();
		que.pop();

		int x = get<0>(t) / 1000;
		int y = get<0>(t) % 1000;
		int hands = get<1>(t);
		char c = get<2>(t);

		if (c == 'k') {
			for (int i = 0; i < 8; i++) {
				int nx = x + k_dx[i];
				int ny = y + k_dy[i];

				if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
					continue;
				}
				if (knight[ny][nx] > hands + 1) {
					knight[ny][nx] = hands + 1;
					if (board[ny][nx] == 'G') {
						cout << hands + 1 << endl;
// show(knight);
// show(bishop);
						return 0;
					}
					que.push(make_tuple(nx * 1000 + ny, hands + 1, board[ny][nx] == 'R' ? 'b' : 'k'));
				}
			}
		} else {
			for (int i = 0; i < 4; i++) {
				int nx = x + b_dx[i];
				int ny = y + b_dy[i];

				if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
					continue;
				}
				if (bishop[ny][nx] > hands + 1) {
					bishop[ny][nx] = hands + 1;
					if (board[ny][nx] == 'G') {
						cout << hands + 1 << endl;
// show(knight);
// show(bishop);
						return 0;
					}
					que.push(make_tuple(nx * 1000 + ny, hands + 1, board[ny][nx] == 'R' ? 'k' : 'b'));
				}
			}
		}
	}

	cout << -1 << endl;
	return 0;
}
0