結果

問題 No.5015 Escape from Labyrinth
ユーザー FplusFplusF
提出日時 2023-04-15 12:30:33
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 3,000 ms
コード長 2,014 bytes
コンパイル時間 2,763 ms
コンパイル使用メモリ 212,508 KB
実行使用メモリ 4,376 KB
スコア 11,870
最終ジャッジ日時 2023-04-15 12:30:43
合計ジャッジ時間 9,166 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 100
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
const ll INF=(1ll<<60);
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
int n,d,h;
vector<string> s;
vector<int> pi={0,1,0,-1},pj={1,0,-1,0};
string dir="RDLU";
string bfs(int si,int sj,int gi,int gj){
    string ret;
    queue<pair<int,int>> q;
    q.push({gi,gj});
    vector<vector<int>> dist(n,vector<int>(n,-1));
    dist[gi][gj]=0;
    while(!q.empty()){
        int ni,nj;
        tie(ni,nj)=q.front();
        q.pop();
        rep(k,4){
            int ti=ni+pi[k],tj=nj+pj[k];
            if(ti<0||n<=ti||tj<0||n<=tj) continue;
            if(s[ti][tj]=='#'||s[ti][tj]=='E') continue;
            if(dist[ti][tj]==-1){
                dist[ti][tj]=dist[ni][nj]+1;
                q.push({ti,tj});
            }
        }
    }
    int ni=si,nj=sj;
    while(ni!=gi||nj!=gj){
        rep(k,4){
            int ti=ni+pi[k],tj=nj+pj[k];
            if(ti<0||n<=ti||tj<0||n<=tj) continue;
            if(dist[ti][tj]==dist[ni][nj]-1){
                ni=ti;
                nj=tj;
                ret.push_back(dir[k]);
                break;
            }
        }
    }
    return ret;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> d >> h;
    s.resize(n);
    rep(i,n) cin >> s[i];
    int si=0,sj=0,ki=0,kj=0,gi=0,gj=0;
    rep(i,n){
        rep(j,n){
            if(s[i][j]=='S'){
                si=i;
                sj=j;
            }
            if(s[i][j]=='K'){
                ki=i;
                kj=j;
            }
            if(s[i][j]=='G'){
                gi=i;
                gj=j;
            }
        }
    }
    string root;
    root+=bfs(si,sj,ki,kj);
    root+=bfs(ki,kj,gi,gj);
    for(auto &i:root){
        cout << "M " << i << '\n';
    }
}
0