結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
kapo
|
| 提出日時 | 2016-05-19 09:32:29 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,647 bytes |
| コンパイル時間 | 746 ms |
| コンパイル使用メモリ | 72,736 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-10-06 05:55:54 |
| 合計ジャッジ時間 | 1,517 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 23 |
ソースコード
#include <iostream>
#include <tuple>
#include <queue>
#include <string>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define tpl tuple<bool, int, int, int>
int main(void)
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int h, w, sy, sx, gy, gx, n, qy, qx, x, y;
string s[500];
bool b[500][500][2], f;
int km[8][2] = { {2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1} };
int bm[4][2] = { {1,1},{-1,1},{-1,-1},{1,-1} };
queue<tpl> q;// is_knight node y x
cin >> h >> w;
rep(i,0,h) cin >> s[i];
rep(i,0,h) rep(j,0,w) {
if(s[i][j]=='S') { sy = i; sx = j; }
if(s[i][j]=='G') { gy = i; gx = j; }
}
q.push(make_tuple(true,0,sy,sx));
while(q.size()) {
f = get<0>(q.front());
n = get<1>(q.front());
qy = get<2>(q.front());
qx = get<3>(q.front());
q.pop();
if(f) {
rep(i,0,8) {
y = qy+km[i][0]; x = qx+km[i][1];
if(y>=0 && y<h && x>=0 && x<w && b[y][x][0]) {
if(s[y][x] == 'G') {
cout << n+1 << endl;
return 0;
}
if(s[y][x] == 'R') {
q.push(make_tuple(!f,n+1,y,x));
b[y][x][0] = false;
}
else {
q.push(make_tuple(f,n+1,y,x));
b[y][x][0] = false;
}
}
}
}
else if(!f) {
rep(i,0,4) {
y = qy+bm[i][0]; x = qx+bm[i][1];
if(y>=0 && y<h && x>=0 && x<w && b[y][x][1]) {
if(s[y][x] == 'G') {
cout << n+1 << endl;
return 0;
}
if(s[y][x] == 'R') {
q.push(make_tuple(!f,n+1,y,x));
b[y][x][1] = false;
}
else {
q.push(make_tuple(f,n+1,y,x));
b[y][x][1] = false;
}
}
}
}
}
cout << -1 << endl;
return 0;
}
kapo