結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
ei1333333
|
| 提出日時 | 2016-04-29 23:23:34 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 30 ms / 2,000 ms |
| コード長 | 1,326 bytes |
| コンパイル時間 | 1,517 ms |
| コンパイル使用メモリ | 164,180 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-04 19:00:33 |
| 合計ジャッジ時間 | 2,023 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:48:26: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
48 | cout << bfs(sx, sy, 'G') << endl;
| ^
main.cpp:48:26: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
int W, H;
char mas[500][500];
int min_cost[2][500][500];
int bfs(int sx, int sy, char c)
{
static const int vx[] = {-1, 1, -2, 2, -2, 2, -1, 1}, vy[] = {-2, -2, -1, -1, 1, 1, 2, 2};
static const int dx[] = {-1, 1, -1, 1}, dy[] = {-1, -1, 1, 1};
memset(min_cost, -1, sizeof(min_cost));
queue< pair< bool, pair< int, int > > > que;
que.push({0, {sx, sy}});
min_cost[0][sx][sy] = 0;
while(!que.empty()) {
auto p = que.front().second;
int type = que.front().first;
que.pop();
if(mas[p.second][p.first] == c) return(min_cost[type][p.first][p.second]);
for(int i = 0; i < (type ? 4 : 8); i++) {
int nx = p.first + (type ? dx[i] : vx[i]);
int ny = p.second + (type ? dy[i] : vy[i]);
if(nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
bool flip = mas[ny][nx] == 'R';
if(min_cost[type ^ flip][nx][ny] != -1) continue;
min_cost[type ^ flip][nx][ny] = min_cost[type][p.first][p.second] + 1;
que.push({type ^ flip, {nx, ny}});
}
}
return(-1);
}
int main()
{
cin >> H >> W;
int sx, sy;
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++) {
cin >> mas[i][j];
if(mas[i][j] == 'S') sx = j, sy = i;
}
}
cout << bfs(sx, sy, 'G') << endl;
}
ei1333333