結果
問題 |
No.367 ナイトの転身
|
ユーザー |
![]() |
提出日時 | 2016-07-13 16:49:48 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,279 bytes |
コンパイル時間 | 621 ms |
コンパイル使用メモリ | 70,940 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-14 15:41:13 |
合計ジャッジ時間 | 1,895 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 13 WA * 14 |
ソースコード
#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]; 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()) { bool koma = false; 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'){ if(koma == true){ for(int i=0; i<8; i++) { int mx = x + nxmove[i]; int my = y + nymove[i]; if(mx < 0 || my < 0 || mx > w || my > h) continue; if(INF == ary[my][mx]){ koma = false; que.push(P(mx,my)); ary[my][mx] = ary[y][x] + 1; //koma = false; } } } for(int i = 0; i < 4; i++) { int mx = x + bxmove[i]; int my = y + bymove[i]; if(mx < 0 || my < 0 || mx > w || my > h) continue; if(INF == ary[my][mx]){ koma = true; que.push(P(mx,my)); ary[my][mx] = ary[y][x] + 1; //koma = true; } } } for(int i=0; i<8; i++) { int mx = x + nxmove[i]; int my = y + nymove[i]; if(mx < 0 || my < 0 || mx > w || my > h) continue; if(INF == ary[my][mx]){ koma = false; que.push(P(mx,my)); ary[my][mx] = ary[y][x] + 1; //koma = false; } } } 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; }