結果

問題 No.367 ナイトの転身
ユーザー parukiparuki
提出日時 2016-04-29 22:58:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,911 bytes
コンパイル時間 1,736 ms
コンパイル使用メモリ 156,332 KB
実行使用メモリ 6,120 KB
最終ジャッジ日時 2023-07-27 18:54:30
合計ジャッジ時間 3,242 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 28 ms
5,888 KB
testcase_11 AC 40 ms
6,120 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 6 ms
4,380 KB
testcase_14 AC 14 ms
4,536 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 14 ms
4,728 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 4 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define rt return
#define FOR(i,j,k) for(int i=j; i<(int)k;++i)
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

// y, x, state, dis
typedef tuple<int, int, int, int> T;
#define N_MAX 501
int vis[N_MAX][N_MAX][2], H, W, sy = -1, sx = -1, gy = -1, gx = -1;
string bo[N_MAX];
// knight
const int DY[8] = {-2,-1,1,2,2,1,-1,-2};
const int DX[8] = {1,2,2,1,-1,-2,-2,-1};

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    queue<T> Q;
    cin >> H >> W;
    rep(i, H)cin >> bo[i];
    rep(y, H)rep(x, W){
        if(bo[y][x] == 'S')sy = y, sx = x;
        if(bo[y][x] == 'G')gy = y, gx = x;
    }
    Q.push(T(sy, sx, 0, 0));
    while(sz(Q)){
        int y, x, stat, d;
        T t = Q.front();
        Q.pop();
        tie(y, x, stat, d) = t;
        if(vis[y][x][stat]++)continue;
        if(y == gy&&x == gx){
            cout << d << endl;
            rt 0;
        }
        if(bo[y][x] == 'R')stat = 1 - stat;
        if(stat == 0){
            rep(i, 8){
                int dy = DY[i], dx = DX[i], ny = y + dy, nx = x + dx;
                if(ny >= 0 && nx >= 0 && ny < H&&nx < W){
                    Q.push(T(ny, nx, stat, d + 1));
                }
            }
        } else{
            for(int dy = -1; dy <= 1; dy += 2)for(int dx = -1; dx <= 1; dx += 2){
                int ny = y + dy, nx = x + dx;
                if(ny >= 0 && nx >= 0 && ny < H&&nx < W){
                    Q.push(T(ny, nx, stat, d + 1));
                }
            }
        }
    }
    cout << -1 << endl;
}
0