結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
yasu_likes_cats
|
| 提出日時 | 2016-07-08 18:46:56 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,986 bytes |
| コンパイル時間 | 743 ms |
| コンパイル使用メモリ | 70,936 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-12 23:30:06 |
| 合計ジャッジ時間 | 1,964 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 WA * 18 |
ソースコード
#include <iostream>
#include <queue>
#include <string>
#include <map>
const int INF = 10000000;
using namespace std;
typedef pair<int,int> P;
int ary[550][550];
int nxmove[8] = {1,-1,1,-1,2,-2,2,-2};
int nymove[8] = {2,2,-2,-2,1,1,-1,-1};
int bxmove[4] = {1,-1,1,-1};
int bymove[4] = {1,1,-1,-1};
int sx ,sy,gx,gy;
int h,w;
string str[1010];
bool koma = 0;
int bfs()
{
queue<P> que;
for(int i=0; i<h; i++)
for(int j=0; j<w; j++)
ary[i][j] = INF;
que.push(P(sx,sy));
ary[sy][sx] = 0;
while(!que.empty())
{
P p = que.front();
que.pop();
int x = p.first;
int y = p.second;
if(x == gx && y == gy)
break;
if(str[y][x] == 'R'){
for(int i = 0; i < 4; i++)
{
if(koma == 1){
for(int i=0; i<8; i++)
{
int mx = x + nxmove[i];
int my = y + nymove[i];
koma = 0;
if(mx < 0 || my < 0 || mx >= w || my >= h)
continue;
if(INF == ary[my][mx])
que.push(P(mx,my));
ary[my][mx] = ary[y][x] + 1;
continue;
}
}
int mx = x + bxmove[i];
int my = y + bymove[i];
koma = 0;
if(mx < 0 || my < 0 || mx >= w || my >= h)
continue;
if(INF == ary[my][mx])
que.push(P(mx,my));
ary[my][mx] = ary[y][x] + 1;
}
}
for(int i=0; i<8; i++)
{
int mx = x + nxmove[i];
int my = y + nymove[i];
koma = 0;
if(mx < 0 || my < 0 || mx >= w || my >= h)
continue;
if(INF == ary[my][mx])
que.push(P(mx,my));
ary[my][mx] = ary[y][x] + 1;
}
}
return ary[gy][gx];
}
int main()
{
cin >> h >> w;
for(int i = 0; i < h; i++)
{
cin >> str[i];
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++)
{
if(str[i][j] == 'S')
{
sx = j;
sy = i;
}
else if(str[i][j] == 'G')
{
gx = j;
gy = i;
}
}
}
}
int ans = bfs();
if(ans != INF)
cout << ans << endl;
else
cout << -1 << endl;
return 0;
}
yasu_likes_cats