Submission #74878294


Source Code Expand

#include<bits/stdc++.h>
// #include<atcoder/all>
using namespace std;
using ll = long long;
#define rep(i, n) for(int  i = 0; i < (n); ++i)
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
const long long INF = 1LL << 60;
using Graph = vector<vector<int>>;
int gcd(int a, int b){
    if(b == 0) return a;  
    else return gcd(b, a%b);
}
struct Compare {
    bool operator()(const pair<ll, int>& a, const pair<ll, int>& b) {
        return a.first > b.first;  
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int h,w; cin >> h >> w;
    vector<vector<char>> s(h, vector<char>(w));
    int sx,sy,gx,gy;
    rep(i,h){
        rep(j,w){
            cin >> s[i][j];
            if(s[i][j] == 'S'){
                sx = i, sy = j;
            }
            if(s[i][j] == 'G'){
                gx = i, gy = j;
            }
        }
    }
    vector<int> dx = {-1,0,1,0}, dy = {0,1,0,-1};
    vector<vector<vector<ll>>> dp(h, vector<vector<ll>>(w, vector<ll>(4, INF)));
    rep(i,4) dp[sx][sy][i] = 0;
    auto valid = [&](int x, int y){
        if(x < 0 || x >= h || y<0 || y>=w) return false;
        return true;
    };
    priority_queue<tuple<ll, int,int,int>, vector<tuple<ll,int,int,int>>, greater<tuple<ll,int,int,int>>> pq;
    rep(i,4) pq.push(make_tuple(0LL, sx,sy,i));
    while(!pq.empty()){
        auto [dist, x, y, dir] = pq.top();
        pq.pop();
        if(dist > dp[x][y][dir]) continue;
        if(dist > 5000000) continue;
        if(s[x][y] == 'o'){
            int nx = x+dx[dir],ny = y+dy[dir];
            if(!valid(nx, ny)) continue;
            if(s[nx][ny] == '#') continue;
            if(chmin(dp[nx][ny][dir], dist+1)){
                if(s[nx][ny] == 'G') continue;
                pq.push(make_tuple(dp[nx][ny][dir], nx, ny, dir));
            }
        }else{
            rep(i,4){
                if(s[x][y] == 'x' && dir == i) continue;
                int nx = x + dx[i], ny = y+dy[i];
                if(!valid(nx, ny)) continue;
                if(s[nx][ny] == '#') continue;
                if(chmin(dp[nx][ny][i], dist+1)){
                    if(s[nx][ny] == 'G') continue;
                    pq.push(make_tuple(dp[nx][ny][i], nx, ny, i)); 
                }
            }
        }
    }
    bool ok = false;
    rep(i,4){
        if(dp[gx][gy][i] <= 5000000) ok = true;
    }
    vector<char> di = {'U', 'R', 'D', 'L'};
    // rep(i,h){
    //   rep(j,w){
    //     rep(k,4){
    //       cout << dp[i][j][k];
    //     }
    //     cout <<" ";
    //   }
    //   cout << endl;
    // }
    if(ok){
        cout << "Yes\n";
        ll now_dist = INF;
        int now_dir = 0;
        rep(i,4){
            if(chmin(now_dist, dp[gx][gy][i])){
                now_dir = i;
            }
        }
        int npx = gx, npy = gy;
        vector<char> ans;
        while(1){
            if(s[npx][npy] == 'S') break;
            int nx = npx - dx[now_dir], ny = npy - dy[now_dir];
            ans.push_back(di[now_dir]);
            if(s[nx][ny] == 'o'){
                npx = nx;
                npy = ny;
                now_dist--;
            }else{
                rep(i,4){
                    if(s[nx][ny] == 'x' && i == now_dir) continue;
                    if(dp[nx][ny][i] == now_dist - 1){
                        now_dir = i;
                        now_dist--;
                        npx = nx;
                        npy = ny;
                        break;
                    }
                }
            }
        }
        reverse(ans.begin(), ans.end());
        for(auto c:ans){
            cout << c;
        }
    }else{
        cout << "No\n";
    }
}

Submission Info

Submission Time
Task D - Go Straight
User motomoto0001
Language C++23 (GCC 15.2.0)
Score 425
Code Size 3906 Byte
Status AC
Exec Time 585 ms
Memory 76976 KiB

Compile Error

./Main.cpp: In function 'main':
./Main.cpp:74:17: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
   74 |         if(dp[gx][gy][i] <= 5000000) ok = true;
      |                 ^
./Main.cpp:25:15: note: 'gx' was declared here
   25 |     int sx,sy,gx,gy;
      |               ^
./Main.cpp:74:21: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
   74 |         if(dp[gx][gy][i] <= 5000000) ok = true;
      |                     ^
./Main.cpp:25:18: note: 'gy' was declared here
   25 |     int sx,sy,gx,gy;
      |                  ^
./Main.cpp:39:19: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
   39 |     rep(i,4) dp[sx][sy][i] = 0;
      |                   ^
./Main.cpp:25:9: note: 'sx' was declared here
   25 |     int sx,sy,gx,gy;
      |         ^
./Main.cpp:39:23: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
   39 |     rep(i,4) dp[sx][sy][i] = 0;
      |                       ^
./Main.cpp:25:12: note: 'sy' was declared here
   25 |     int sx,sy,gx,gy;
      |            ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 3
AC × 46
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3564 KiB
example_01.txt AC 1 ms 3616 KiB
example_02.txt AC 1 ms 3616 KiB
hand_00.txt AC 579 ms 74952 KiB
hand_01.txt AC 63 ms 74752 KiB
hand_02.txt AC 285 ms 76976 KiB
hand_03.txt AC 250 ms 76844 KiB
hand_04.txt AC 1 ms 3564 KiB
hand_05.txt AC 585 ms 74928 KiB
hand_06.txt AC 120 ms 75940 KiB
hand_07.txt AC 583 ms 74940 KiB
hand_08.txt AC 64 ms 74752 KiB
hand_09.txt AC 1 ms 3644 KiB
random_00.txt AC 60 ms 71368 KiB
random_01.txt AC 344 ms 72784 KiB
random_02.txt AC 320 ms 68044 KiB
random_03.txt AC 61 ms 70340 KiB
random_04.txt AC 345 ms 72776 KiB
random_05.txt AC 228 ms 65104 KiB
random_06.txt AC 63 ms 72132 KiB
random_07.txt AC 61 ms 71108 KiB
random_08.txt AC 256 ms 71116 KiB
random_09.txt AC 273 ms 74300 KiB
random_10.txt AC 232 ms 62908 KiB
random_11.txt AC 257 ms 67644 KiB
random_12.txt AC 263 ms 68292 KiB
random_13.txt AC 54 ms 65352 KiB
random_14.txt AC 238 ms 67144 KiB
random_15.txt AC 56 ms 65480 KiB
random_16.txt AC 56 ms 68424 KiB
random_17.txt AC 52 ms 63408 KiB
random_18.txt AC 53 ms 64712 KiB
random_19.txt AC 61 ms 72012 KiB
random_20.txt AC 55 ms 65340 KiB
random_21.txt AC 73 ms 63944 KiB
random_22.txt AC 54 ms 66032 KiB
random_23.txt AC 61 ms 71892 KiB
random_24.txt AC 71 ms 71236 KiB
random_25.txt AC 59 ms 66416 KiB
random_26.txt AC 54 ms 64068 KiB
random_27.txt AC 292 ms 71540 KiB
random_28.txt AC 58 ms 68428 KiB
random_29.txt AC 64 ms 64372 KiB
random_30.txt AC 135 ms 68540 KiB
random_31.txt AC 109 ms 72576 KiB
random_32.txt AC 65 ms 66508 KiB