結果

問題 No.367 ナイトの転身
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-04 18:33:21
言語 C++11
(gcc 11.4.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,083 bytes
コンパイル時間 884 ms
コンパイル使用メモリ 85,252 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-14 12:24:23
合計ジャッジ時間 4,627 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 6 ms
6,940 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 3 ms
6,940 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 3 ms
6,940 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:111:22: warning: ‘gy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  111 |         int ans = bfs(sx, sy, gx, gy);
      |                   ~~~^~~~~~~~~~~~~~~~
main.cpp:111:22: warning: ‘gx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
main.cpp:111:22: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
main.cpp:111:22: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
#include <string>
using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define FORR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define rep(i,n) for (int i=0;i<(n);i++)
#define Rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

#define PI 3.1415926535

typedef long long ll;
typedef pair<int, int> P;

const int INF = 99999999;
const int MAX_H = 500;
const int MAX_W = 500;

int H, W;
// 'S'(開始位置), 'G'(目標位置), 'R'(赤いマス), '.'(ただのマス)のいずれか
// 'S', 'G' は普通のマスであることが保証されている
// 駒は最初はナイト
// 赤いマスに乗ったとき、ナイトならミニビショップに、ミニビショップならナイトに変わる
vector<string> field;

// 1=knight, 0=mini-bishop
int d[MAX_H][MAX_W][2];

// knight
int dx8[8] = {2, 2, 1, 1, -1, -1, -2, -2};
int dy8[8] = {1, -1, 2, -2, 2, -2, 1, -1};

// mini-bishop
int dx4[4] = {1, 1, -1, -1};
int dy4[4] = {1, -1, -1, 1};

bool inside(int x, int y) {
    return x >= 0 && x < H && y >= 0 && y < W;
}

int bfs(int sx, int sy, int gx, int gy) {
	// 駒の種類(1=knight, 0=mini-bishop), 座標
    queue<pair<int, P>> que;
	static int d[MAX_H][MAX_W][2];
    rep(i, H) rep(j, W) rep(k, 2) d[i][j][k] = INF;
    que.push(make_pair(1, P(sx, sy)));
    d[sx][sy][1] = 0;

    while (!que.empty()) {
		int mode = que.front().first;	// 駒の種類
        P p = que.front().second;		// 座標
		que.pop();
        if (p.first == gx && p.second == gy)
             break;

		if (mode == 1) {
			// マスpからknightの利きで移動するとき
	        rep(k, 8) {
	            int nx = p.first + dx8[k], ny = p.second + dy8[k];
				// 次のマスが赤だったら、次のマスでは駒が変わる
				int nm = mode ^ (field[nx][ny] == 'R');
	            if (inside(nx, ny) && d[nx][ny][nm] == INF) {
	                que.push(make_pair(nm, P(nx, ny)));
	                d[nx][ny][nm] = d[p.first][p.second][mode] + 1;
	            }
	        }
		} else {
			// マスpからmini-bishopの利きで移動するとき
			rep(k, 4) {
	            int nx = p.first + dx4[k], ny = p.second + dy4[k];
				// 次のマスが赤だったら、次のマスでは駒が変わる
				int nm = mode ^ (field[nx][ny] == 'R');
	            if (inside(nx, ny) && d[nx][ny][nm] == INF) {
	                que.push(make_pair(nm, P(nx, ny)));
	                d[nx][ny][nm] = d[p.first][p.second][mode] + 1;
	            }
	        }
		}
    }
    return min(d[gx][gy][0], d[gx][gy][1]);
}

int main() {
	cin >> H >> W;

	field.clear();
	field.resize(H);

	// 盤面の読み込み & スタート位置、ゴール位置の取得
	int sx, sy, gx, gy;
	rep(i, H) {
		cin >> field[i];
		rep(j, W) {
			if (field[i][j] == 'S') {
				sx = i;
				sy = j;
			} else if (field[i][j] == 'G') {
				gx = i;
				gy = j;
			}
		}
	}

	int ans = bfs(sx, sy, gx, gy);
	if (ans == INF) ans = -1;
	cout << ans << endl;

}
0