Submission #2947402


Source Code Expand

Copy
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/






int H, W, K; string D, S[1010];
ll wait[101010][4];
//---------------------------------------------------------------------------------------------------
map<char, int> dir = { { 'U', 0 },{ 'R', 1 },{ 'D', 2 },{ 'L', 3 } };
enum { UP = 0, RIGHT, DOWN, LEFT };
int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
//---------------------------------------------------------------------------------------------------
void make_wait() {
    rep(k, 0, K) rep(d, 0, 4) wait[k][d] = infl;

    rep(k, 0, K) wait[k][dir[D[k]]] = 0;
    rep(k, 0, K) {
        int d = dir[D[k]];
        int i = (k - 1 + K) % K;
        ll c = 1;
        while (wait[i][d] == infl) {
            wait[i][d] = c;
            c++;
            i = (i - 1 + K) % K;
        }
    }
}
//---------------------------------------------------------------------------------------------------
ll dp[1010][1010]; int vis[1010][1010];
ll dijk(int sx, int sy) {
    min_priority_queue<pair<ll, int>> que;

    rep(y, 0, H) rep(x, 0, W) dp[y][x] = infl;
    rep(y, 0, H) rep(x, 0, W) vis[y][x] = 0;

    dp[sy][sx] = 0;
    que.push({ 0, sy * 1010 + sx });

    while (!que.empty()) {
        auto q = que.top();
        que.pop();

        int x = q.second % 1010;
        int y = q.second / 1010;
        ll c = q.first;

        if (vis[y][x]) continue;
        vis[y][x] = 1;
        
        if (S[y][x] == 'G') return c;

        rep(d, 0, 4) {
            int xx = x + dx[d];
            int yy = y + dy[d];
            if (0 <= xx and xx < W and 0 <= yy and yy < H) {
                if (S[yy][xx] != '#') {
                    ll cc = c + wait[c%K][d] + 1;
                    if (cc < dp[yy][xx]) {
                        dp[yy][xx] = cc;
                        que.push({ cc, yy * 1010 + xx });
                    }
                }
            }
        }
    }

    return -1;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> H >> W >> K >> D;
    rep(y, 0, H) cin >> S[y];

    make_wait();
    
    int sx, sy;
    rep(y, 0, H) rep(x, 0, W) if (S[y][x] == 'S') sx = x, sy = y;
    ll ans = dijk(sx, sy);
    printf("%lld\n", ans);
}

Submission Info

Submission Time
Task E - 迷路
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 500
Code Size 3610 Byte
Status
Exec Time 138 ms
Memory 16592 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0
All 500 / 500 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt 2 ms 4352 KB
02.txt 2 ms 4352 KB
03.txt 1 ms 4352 KB
04.txt 2 ms 4352 KB
05.txt 2 ms 4352 KB
06.txt 119 ms 16592 KB
07.txt 94 ms 16592 KB
08.txt 110 ms 16592 KB
09.txt 99 ms 16592 KB
10.txt 93 ms 16592 KB
11.txt 94 ms 16592 KB
12.txt 138 ms 16592 KB
13.txt 119 ms 16592 KB
14.txt 132 ms 16592 KB
15.txt 114 ms 16592 KB
16.txt 17 ms 16592 KB
17.txt 60 ms 16592 KB
18.txt 52 ms 16592 KB
19.txt 32 ms 16592 KB
20.txt 90 ms 16592 KB
21.txt 129 ms 16592 KB
22.txt 126 ms 16592 KB
23.txt 132 ms 16592 KB
24.txt 127 ms 16592 KB
25.txt 131 ms 16592 KB
26.txt 132 ms 16592 KB
27.txt 131 ms 16592 KB
28.txt 128 ms 16592 KB
29.txt 129 ms 16592 KB
30.txt 136 ms 16592 KB
31.txt 13 ms 16464 KB
32.txt 13 ms 16464 KB
33.txt 13 ms 16464 KB
34.txt 13 ms 16464 KB
35.txt 13 ms 16464 KB
36.txt 13 ms 16464 KB
37.txt 12 ms 16464 KB
38.txt 12 ms 16464 KB
39.txt 13 ms 16464 KB
40.txt 38 ms 16464 KB
41.txt 2 ms 4352 KB
s1.txt 2 ms 4352 KB
s2.txt 2 ms 4352 KB
s3.txt 2 ms 4352 KB