結果

問題 No.367 ナイトの転身
ユーザー koyoprokoyopro
提出日時 2020-03-28 00:40:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 2,874 bytes
コンパイル時間 1,588 ms
コンパイル使用メモリ 152,332 KB
実行使用メモリ 10,636 KB
最終ジャッジ日時 2023-08-30 17:09:05
合計ジャッジ時間 3,464 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,520 KB
testcase_01 AC 2 ms
7,564 KB
testcase_02 AC 2 ms
7,476 KB
testcase_03 AC 3 ms
7,512 KB
testcase_04 AC 3 ms
7,512 KB
testcase_05 AC 2 ms
7,740 KB
testcase_06 AC 3 ms
9,484 KB
testcase_07 AC 3 ms
7,476 KB
testcase_08 AC 2 ms
7,732 KB
testcase_09 AC 3 ms
7,480 KB
testcase_10 AC 36 ms
9,612 KB
testcase_11 AC 65 ms
10,636 KB
testcase_12 AC 31 ms
8,980 KB
testcase_13 AC 29 ms
9,048 KB
testcase_14 AC 29 ms
9,260 KB
testcase_15 AC 4 ms
8,964 KB
testcase_16 AC 27 ms
9,528 KB
testcase_17 AC 5 ms
8,060 KB
testcase_18 AC 7 ms
8,076 KB
testcase_19 AC 10 ms
8,208 KB
testcase_20 AC 5 ms
8,132 KB
testcase_21 AC 10 ms
8,192 KB
testcase_22 AC 2 ms
7,632 KB
testcase_23 AC 2 ms
7,604 KB
testcase_24 AC 3 ms
7,660 KB
testcase_25 AC 3 ms
7,816 KB
testcase_26 AC 2 ms
7,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)

int dxy[] = {1,2, 2,1, -1,-2, -2,-1, 1,2};
int dxy2[] = {1,1,-1,-1,1,1};

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}

/*================================*/
#if DEBUG
#define SIZE 4
#else
#define SIZE 555
#endif

int H,W;
int MAP[SIZE][SIZE];
int START=1,GOAL=2,RED=3;

struct P {
    int h,w,t;
    P(int h, int w, int t):h(h),w(w),t(t){};
    
    bool operator < (const P& p) const {
        return p.t > t;
    }
};
typedef pair<int, P> Q;
priority_queue<Q, vector<Q>, greater<Q>> que;
int DP[2][SIZE][SIZE];
int N=0,B=1;
P *goal;

void solve() {
    cin>>H>>W;
    string s;
    REP(i,2)REP(h,H)REP(w,W)DP[i][h][w]=INT_MAX;
    REP(h,H) {
        cin>>s;
        REP(w,W) {
            if (s[w]=='S') {
                MAP[h][w]=START;
                DP[N][h][w]=0;
                que.push(Q(0,P(h,w,N)));
            }
            if (s[w]=='G') {
                MAP[h][w]=GOAL;
                goal = new P(h,w,0);
            }
            if (s[w]=='R') MAP[h][w]=RED;
        }
    }
    while(que.size()) {
        auto item = que.top();
        que.pop();
        auto p = item.second;
        if (DP[p.t][p.h][p.w] < item.first) continue;
        int t = p.t;
        if (MAP[p.h][p.w]==RED) t = !t;
        if (t==N) {
            REP(i,8) {
                int nh = p.h + dxy[i];
                int nw = p.w + dxy[i+2];
                int nc = item.first+1;
                if (nh<0||nw<0||H<=nh||W<=nw) continue;
                if (DP[t][nh][nw] > nc) {
                    DP[t][nh][nw] = nc;
                    que.push(Q(nc, P(nh,nw,t)));
                }
            }
        }
        if (t==B) {
            REP(i,4) {
                int nh = p.h + dxy2[i];
                int nw = p.w + dxy2[i+1];
                int nc = item.first+1;
                if (nh<0||nw<0||H<=nh||W<=nw) continue;
                if (DP[t][nh][nw] > nc) {
                    DP[t][nh][nw] = nc;
                    que.push(Q(nc, P(nh,nw,t)));
                }
            }
        }
    }
    
    int ans = min(DP[N][goal->h][goal->w], DP[B][goal->h][goal->w]);
    if (ans == INT_MAX) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
}
0