結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
ふーらくたる
|
| 提出日時 | 2016-07-06 09:01:18 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 31 ms / 2,000 ms |
| コード長 | 2,179 bytes |
| コンパイル時間 | 591 ms |
| コンパイル使用メモリ | 66,540 KB |
| 実行使用メモリ | 5,888 KB |
| 最終ジャッジ日時 | 2024-10-12 20:25:33 |
| 合計ジャッジ時間 | 1,673 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
コンパイルメッセージ
main.cpp: In function ‘void Solve()’:
main.cpp:44:25: warning: ‘gc’ may be used uninitialized in this function [-Wmaybe-uninitialized]
44 | if (s.row == gr && s.col == gc) {
| ~~~~~~~~~~~~^~~~~~~~~~~~~~
main.cpp:44:9: warning: ‘gr’ may be used uninitialized in this function [-Wmaybe-uninitialized]
44 | if (s.row == gr && s.col == gc) {
| ^~
main.cpp:41:27: warning: ‘sc’ may be used uninitialized in this function [-Wmaybe-uninitialized]
41 | cost[kKNIGHT][sr][sc] = 0;
| ~~~~~~~~~~~~~~~~~~~~~~^~~
main.cpp:41:27: warning: ‘sr’ may be used uninitialized in this function [-Wmaybe-uninitialized]
ソースコード
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct State {
int row, col, piece, cost;
};
const int kMAX_H = 510;
const int kMAX_W = 510;
const int kKNIGHT = 0;
const int kMINI_BISHOP = 1;
string S[kMAX_H];
int H, W;
int cost[2][kMAX_H][kMAX_W];
int n_dr[8] = { 1, 1, -1, -1, 2, 2, -2, -2},
n_dc[8] = { 2, -2, 2, -2, 1, -1, 1, -1};
int b_dr[4] = { 1, 1, -1, -1},
b_dc[4] = { 1, -1, 1, -1};
void Solve() {
queue<State> que;
int sr, sc, gr, gc;
for (int r = 0; r < H; r++) {
for (int c = 0; c < W; c++) {
if (S[r][c] == 'S') {
sr = r; sc = c;
} else if (S[r][c] == 'G') {
gr = r; gc = c;
}
}
}
memset(cost, -1, sizeof(cost));
que.push((State){sr, sc, kKNIGHT, 0});
cost[kKNIGHT][sr][sc] = 0;
while (!que.empty()) {
State s = que.front(); que.pop();
if (s.row == gr && s.col == gc) {
cout << s.cost << endl;
return;
}
if (s.piece == kKNIGHT) {
for (int i = 0; i < 8; i++) {
int nr = s.row + n_dr[i], nc = s.col + n_dc[i];
if (0 <= nr && nr < H && 0 <= nc && nc < W) {
int npiece = S[nr][nc] == 'R' ? kMINI_BISHOP : kKNIGHT;
if (cost[npiece][nr][nc] >= 0) continue;
que.push((State){nr, nc, npiece, s.cost + 1});
cost[npiece][nr][nc] = s.cost + 1;
}
}
} else {
for (int i = 0; i < 4; i++) {
int nr = s.row + b_dr[i], nc = s.col + b_dc[i];
if (0 <= nr && nr < H && 0 <= nc && nc < W) {
int npiece = S[nr][nc] == 'R' ? kKNIGHT : kMINI_BISHOP;
if (cost[npiece][nr][nc] >= 0) continue;
que.push((State){nr, nc, npiece, s.cost + 1});
cost[npiece][nr][nc] = s.cost + 1;
}
}
}
}
cout << -1 << endl;
}
int main() {
cin >> H >> W;
for (int i = 0; i < H; i++) {
cin >> S[i];
}
Solve();
return 0;
}
ふーらくたる