結果

問題 No.367 ナイトの転身
ユーザー imulanimulan
提出日時 2016-07-22 17:33:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 1,724 bytes
コンパイル時間 1,726 ms
コンパイル使用メモリ 176,680 KB
実行使用メモリ 5,812 KB
最終ジャッジ日時 2023-08-06 05:15:16
合計ジャッジ時間 3,268 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 3 ms
5,332 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 23 ms
5,808 KB
testcase_11 AC 33 ms
5,812 KB
testcase_12 AC 24 ms
4,808 KB
testcase_13 AC 15 ms
4,820 KB
testcase_14 AC 20 ms
4,824 KB
testcase_15 AC 3 ms
4,692 KB
testcase_16 AC 20 ms
4,816 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 8 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 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;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pi;
typedef pair<pi,int> P;
typedef vector<int> vi;

const int INF=87654321;

int h,w;
int dist[500][500][2];

vector<vi> dx{ {1,2,2,1,-1,-2,-2,-1},
               {1,1,-1,-1} };
vector<vi> dy{ {-2,-1,1,2,2,1,-1,-2},
               {-1,1,1,-1} };

inline bool in(int y, int x)
{
    return (0<=x&&x<w && 0<=y&&y<h);
}

int main()
{
    cin >>h >>w;
    vector<string> s(h);
    rep(i,h) cin >>s[i];

    pi start,goal;
    rep(i,h)rep(j,w)
    {
        if(s[i][j]=='S') start=pi(i,j);
        if(s[i][j]=='G') goal=pi(i,j);
    }

    rep(i,h)rep(j,w)rep(k,2) dist[i][j][k]=INF;
    dist[start.fi][start.se][0]=0;

    queue<P> que;
    que.push(P(start,0));

    while(!que.empty())
    {
        P p=que.front();
        que.pop();

        pi now=p.fi;
        int st=p.se;
        rep(i,dx[st].size())
        {
            int nx=now.se+dx[st][i];
            int ny=now.fi+dy[st][i];
            if(in(ny,nx))
            {
                int nst=st;
                if(s[ny][nx]=='R') nst=!nst;

                if(dist[ny][nx][nst]>dist[now.fi][now.se][st]+1)
                {
                    dist[ny][nx][nst]=dist[now.fi][now.se][st]+1;
                    que.push(P(pi(ny,nx),nst));
                }
            }
        }
    }

    int ans=min(dist[goal.fi][goal.se][0],dist[goal.fi][goal.se][1]);
    if(ans==INF) ans=-1;
    cout << ans << endl;
    return 0;
}
0