結果
| 問題 | No.367 ナイトの転身 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-04-29 23:26:29 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 16 ms / 2,000 ms | 
| コード長 | 2,246 bytes | 
| コンパイル時間 | 800 ms | 
| コンパイル使用メモリ | 85,420 KB | 
| 実行使用メモリ | 6,820 KB | 
| 最終ジャッジ日時 | 2024-10-04 19:03:42 | 
| 合計ジャッジ時間 | 1,523 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:49:18: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |         knight[sy][sx] = 0;
      |                  ^
main.cpp:49:22: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |         knight[sy][sx] = 0;
      |                      ^
            
            ソースコード
#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;
}
            
            
            
        