結果

問題 No.5015 Escape from Labyrinth
ユーザー FplusFplusFFplusFplusF
提出日時 2023-04-16 13:06:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 12,827 bytes
コンパイル時間 5,080 ms
コンパイル使用メモリ 249,724 KB
実行使用メモリ 63,896 KB
スコア 105,660
最終ジャッジ日時 2023-04-16 13:07:58
合計ジャッジ時間 108,541 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,505 ms
63,568 KB
testcase_01 AC 920 ms
63,568 KB
testcase_02 AC 625 ms
63,572 KB
testcase_03 WA -
testcase_04 AC 341 ms
63,296 KB
testcase_05 AC 1,407 ms
63,560 KB
testcase_06 AC 1,164 ms
63,560 KB
testcase_07 AC 769 ms
63,552 KB
testcase_08 AC 664 ms
63,628 KB
testcase_09 AC 486 ms
63,548 KB
testcase_10 AC 1,854 ms
63,568 KB
testcase_11 AC 1,011 ms
63,700 KB
testcase_12 AC 1,098 ms
63,660 KB
testcase_13 AC 797 ms
63,696 KB
testcase_14 AC 316 ms
63,812 KB
testcase_15 AC 1,985 ms
63,896 KB
testcase_16 AC 1,135 ms
63,628 KB
testcase_17 AC 910 ms
63,616 KB
testcase_18 AC 906 ms
63,836 KB
testcase_19 AC 563 ms
63,460 KB
testcase_20 AC 1,959 ms
63,564 KB
testcase_21 AC 1,014 ms
63,620 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 524 ms
63,504 KB
testcase_25 AC 1,703 ms
63,568 KB
testcase_26 AC 1,118 ms
63,548 KB
testcase_27 AC 844 ms
63,496 KB
testcase_28 WA -
testcase_29 AC 574 ms
63,616 KB
testcase_30 AC 1,619 ms
63,892 KB
testcase_31 AC 1,105 ms
63,712 KB
testcase_32 AC 693 ms
63,560 KB
testcase_33 AC 625 ms
63,692 KB
testcase_34 AC 404 ms
63,524 KB
testcase_35 AC 1,553 ms
63,616 KB
testcase_36 AC 1,152 ms
63,632 KB
testcase_37 AC 623 ms
63,336 KB
testcase_38 AC 841 ms
63,620 KB
testcase_39 AC 395 ms
63,676 KB
testcase_40 AC 1,942 ms
63,892 KB
testcase_41 AC 1,300 ms
63,668 KB
testcase_42 AC 859 ms
63,632 KB
testcase_43 AC 709 ms
63,360 KB
testcase_44 AC 389 ms
63,616 KB
testcase_45 AC 2,015 ms
63,608 KB
testcase_46 AC 1,104 ms
63,616 KB
testcase_47 WA -
testcase_48 AC 652 ms
63,488 KB
testcase_49 AC 510 ms
63,776 KB
testcase_50 WA -
testcase_51 AC 1,275 ms
63,560 KB
testcase_52 AC 625 ms
63,828 KB
testcase_53 AC 647 ms
63,568 KB
testcase_54 AC 477 ms
63,748 KB
testcase_55 AC 1,697 ms
63,816 KB
testcase_56 AC 999 ms
63,896 KB
testcase_57 AC 1,164 ms
63,700 KB
testcase_58 AC 558 ms
63,572 KB
testcase_59 WA -
testcase_60 WA -
testcase_61 AC 1,172 ms
63,680 KB
testcase_62 AC 841 ms
63,648 KB
testcase_63 AC 459 ms
63,404 KB
testcase_64 AC 341 ms
63,556 KB
testcase_65 AC 1,642 ms
63,732 KB
testcase_66 AC 1,186 ms
63,816 KB
testcase_67 AC 1,031 ms
63,648 KB
testcase_68 AC 897 ms
63,612 KB
testcase_69 AC 825 ms
63,660 KB
testcase_70 AC 1,574 ms
63,628 KB
testcase_71 AC 893 ms
63,632 KB
testcase_72 AC 959 ms
63,712 KB
testcase_73 AC 826 ms
63,728 KB
testcase_74 AC 459 ms
63,648 KB
testcase_75 AC 1,229 ms
63,636 KB
testcase_76 AC 1,038 ms
63,620 KB
testcase_77 AC 971 ms
63,680 KB
testcase_78 AC 685 ms
63,824 KB
testcase_79 AC 692 ms
63,552 KB
testcase_80 AC 1,450 ms
63,764 KB
testcase_81 AC 854 ms
63,684 KB
testcase_82 AC 858 ms
63,608 KB
testcase_83 AC 642 ms
63,844 KB
testcase_84 AC 657 ms
63,572 KB
testcase_85 AC 1,162 ms
63,624 KB
testcase_86 AC 1,135 ms
63,568 KB
testcase_87 AC 1,116 ms
63,516 KB
testcase_88 AC 723 ms
63,616 KB
testcase_89 AC 481 ms
63,552 KB
testcase_90 AC 1,779 ms
63,284 KB
testcase_91 AC 960 ms
63,552 KB
testcase_92 AC 503 ms
63,752 KB
testcase_93 AC 833 ms
63,576 KB
testcase_94 AC 771 ms
63,472 KB
testcase_95 AC 1,593 ms
63,496 KB
testcase_96 AC 1,042 ms
63,388 KB
testcase_97 AC 1,043 ms
63,644 KB
testcase_98 AC 572 ms
63,440 KB
testcase_99 AC 262 ms
63,624 KB
権限があれば一括ダウンロードができます

ソースコード

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;
    }
}
auto start=chrono::system_clock::now();
mt19937 mt;
vector<int> pi={0,1,0,-1},pj={1,0,-1,0};
string dir="RDLU";
struct enemy{
    int i,j,d;
};
struct input{
    int n,d,h;
    vector<string> s;
    int m;
    vector<enemy> e;
    vector<vector<int>> grid_enemy;
    int si,sj,ki,kj,gi,gj;
    void input_cin(){
        cin >> n >> d >> h;
        s.resize(n);
        rep(i,n) cin >> s[i];
        cin >> m;
        e.resize(m);
        grid_enemy.resize(n);
        rep(i,n) grid_enemy[i].resize(n,0);
        rep(i,m){
            cin >> e[i].i >> e[i].j >> e[i].d;
            grid_enemy[e[i].i][e[i].j]=e[i].d;
        }
        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;
                }
            }
        }
    }
}
input;
struct solver{
    struct result{
        int score,hp;
        vector<vector<bool>> visited;
    };
    bool outside(int i,int j){
        if(i<0||input.n<=i||j<0||input.n<=j) return true;
        else return false;
    }
    int char_idx(char c){
        if(c=='R') return 0;
        else if(c=='D') return 1;
        else if(c=='L') return 2;
        else return 3;
    }
    int dist_root(vector<pair<int,int>> &v,vector<vector<vector<vector<int>>>> &dist_juwel){
        int n=v.size();
        int ret=0;
        rep(i,n-1){
            int d=dist_juwel[v[i].first][v[i].second][v[i+1].first][v[i+1].second];
            if(d==-1) return 1e9;
            ret+=d;
        }
        return ret;
    }
    result play(string s){
        int nt=s.size();
        int ni=input.si,nj=input.sj;
        vector<vector<bool>> visited(input.n,vector<bool>(input.n));
        result ret={0,input.h,visited};
        ret.visited[ni][nj]=true;
        rep(t,nt){
            ni+=pi[char_idx(s[t])];
            nj+=pj[char_idx(s[t])];
            if(outside(ni,nj)){
                ret.score=-1e9;
                ret.hp=-1e9;
                return ret;
            }
            if(!ret.visited[ni][nj]&&input.s[ni][nj]=='J') ret.score+=10;
            ret.visited[ni][nj]=true;
            ret.hp--;
            for(int i=ni;0<=i;i--){
                if(input.s[i][nj]=='#') break;
                if(input.s[i][nj]=='E'){
                    if((t+1)%input.grid_enemy[i][nj]==0) ret.hp-=input.d;
                    break;
                }
            }
            for(int i=ni;i<input.n;i++){
                if(input.s[i][nj]=='#') break;
                if(input.s[i][nj]=='E'){
                    if((t+1)%input.grid_enemy[i][nj]==0) ret.hp-=input.d;
                    break;
                }
            }
            for(int j=nj;0<=j;j--){
                if(input.s[ni][j]=='#') break;
                if(input.s[ni][j]=='E'){
                    if((t+1)%input.grid_enemy[ni][j]==0) ret.hp-=input.d;
                    break;
                }
            }
            for(int j=nj;j<input.n;j++){
                if(input.s[ni][j]=='#') break;
                if(input.s[ni][j]=='E'){
                    if((t+1)%input.grid_enemy[ni][j]==0) ret.hp-=input.d;
                    break;
                }
            }
            if(ret.hp<=0){
                ret.score=-1e9;
                ret.hp=-1e9;
                return ret;
            }
        }
        if(ni==input.gi&&nj==input.gj) ret.hp++;
        else{
            ret.score=-1e9;
            //ret.hp=0;
        }
        return ret;
    }
    result play_free(int si,int sj,string &s){
        int nt=s.size();
        int ni=si,nj=sj;
        vector<vector<bool>> visited(input.n,vector<bool>(input.n));
        result ret={0,input.h,visited};
        ret.visited[ni][nj]=true;
        rep(t,nt){
            ni+=pi[char_idx(s[t])];
            nj+=pj[char_idx(s[t])];
            if(outside(ni,nj)){
                ret.score=-1e9;
                ret.hp=-1e9;
                return ret;
            }
            if(!ret.visited[ni][nj]&&input.s[ni][nj]=='J') ret.score+=10;
            ret.visited[ni][nj]=true;
            ret.hp--;
            for(int i=ni;0<=i;i--){
                if(input.s[i][nj]=='#') break;
                if(input.s[i][nj]=='E'){
                    if((t+1)%input.grid_enemy[i][nj]==0) ret.hp-=input.d;
                    break;
                }
            }
            for(int i=ni;i<input.n;i++){
                if(input.s[i][nj]=='#') break;
                if(input.s[i][nj]=='E'){
                    if((t+1)%input.grid_enemy[i][nj]==0) ret.hp-=input.d;
                    break;
                }
            }
            for(int j=nj;0<=j;j--){
                if(input.s[ni][j]=='#') break;
                if(input.s[ni][j]=='E'){
                    if((t+1)%input.grid_enemy[ni][j]==0) ret.hp-=input.d;
                    break;
                }
            }
            for(int j=nj;j<input.n;j++){
                if(input.s[ni][j]=='#') break;
                if(input.s[ni][j]=='E'){
                    if((t+1)%input.grid_enemy[ni][j]==0) ret.hp-=input.d;
                    break;
                }
            }
            if(ret.hp<=0) return {(int)-1e9,(int)-1e9};
        }
        if(ni==input.gi&&nj==input.gj) ret.hp++;
        else{
            ret.score=-1e9;
            //ret.hp=0;
        }
        return ret;
    }
    vector<vector<int>> bfs(int si,int sj){
        queue<pair<int,int>> q;
        q.push({si,sj});
        vector<vector<int>> dist(input.n,vector<int>(input.n,-1));
        dist[si][sj]=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(outside(ti,tj)) continue;
                if(input.s[ti][tj]=='#'||input.s[ti][tj]=='E') continue;
                if(dist[ti][tj]==-1){
                    dist[ti][tj]=dist[ni][nj]+1;
                    q.push({ti,tj});
                }
            }
        }
        return dist;
    }
    string min_path(int si,int sj,int gi,int gj){
        string ret;
        queue<pair<int,int>> q;
        q.push({gi,gj});
        vector<vector<int>> dist(input.n,vector<int>(input.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(outside(ti,tj)) continue;
                if(input.s[ti][tj]=='#'||input.s[ti][tj]=='E') continue;
                if(dist[ti][tj]==-1){
                    dist[ti][tj]=dist[ni][nj]+1;
                    q.push({ti,tj});
                }
            }
        }
        if(dist[si][sj]==-1) return "";
        int ni=si,nj=sj;
        while(ni!=gi||nj!=gj){
            int x=0;
            rep(k,4){
                int ti=ni+pi[k],tj=nj+pj[k];
                if(outside(ti,tj)) continue;
                if(dist[ti][tj]==dist[ni][nj]-1){
                    x=k;
                    if(input.s[ti][tj]=='J') break;
                }
            }
            int ti=ni+pi[x],tj=nj+pj[x];
            ni=ti;
            nj=tj;
            ret.push_back(dir[x]);
        }
        return ret;
    }
    string min_path_v(int si,int sj,int gi,int gj,vector<pair<int,int>> &v){
        if((int)v.size()==0){
            return min_path(si,sj,gi,gj);
        }
        string ret;
        ret+=min_path(si,sj,v.front().first,v.front().second);
        rep(i,(int)v.size()-1){
            ret+=min_path(v[i].first,v[i].second,v[i+1].first,v[i+1].second);
        }
        ret+=min_path(v.back().first,v.back().second,gi,gj);
        return ret;
    }
    string v_to_root(vector<pair<int,int>> &v){
        string ret;
        rep(i,v.size()-1){
            ret+=min_path(v[i].first,v[i].second,v[i+1].first,v[i+1].second);
        }
        return ret;
    }
    string v_to_root(vector<pair<int,int>> &first_v,int si,int sj,vector<pair<int,int>> &v,int gi,int gj){
        vector<pair<int,int>> new_v;
        for(auto &i:first_v) new_v.emplace_back(i);
        if(new_v.size()==0||(new_v.back().first!=si||new_v.back().second!=sj)) new_v.emplace_back(si,sj);
        for(auto &i:v){
            if(i==new_v.back()) continue;
            new_v.emplace_back(i);
        }
        if(new_v.size()==0||(new_v.back().first!=gi||new_v.back().second!=gj)) new_v.emplace_back(gi,gj);
        return v_to_root(new_v);
    }
    vector<pair<int,int>> v_to_v(vector<pair<int,int>> &first_v,int si,int sj,vector<pair<int,int>> &v,int gi,int gj){
        vector<pair<int,int>> new_v;
        for(auto &i:first_v) new_v.emplace_back(i);
        if(new_v.size()==0||(new_v.back().first!=si||new_v.back().second!=sj)) new_v.emplace_back(si,sj);
        for(auto &i:v){
            if(i==new_v.back()) continue;
            new_v.emplace_back(i);
        }
        if(new_v.size()==0||(new_v.back().first!=gi||new_v.back().second!=gj)) new_v.emplace_back(gi,gj);
        return new_v;
    }
    vector<pair<int,int>> solve_root(vector<pair<int,int>> first_v,int si,int sj,int gi,int gj,int min_hp,vector<vector<vector<vector<int>>>> &dist_juwel){
        vector<pair<int,int>> v;
        int ni=si,nj=sj;
        while(true){
            vector<vector<bool>> visited=play(v_to_root(first_v,si,sj,v,gi,gj)).visited;
            bool first_flag=true,end_flag=false;
            while(true){
                vector<vector<int>> dist=bfs(ni,nj);
                pair<int,pair<int,int>> p={(int)1e9,{0,0}};
                rep(i,input.n){
                    rep(j,input.n){
                        if(dist[i][j]==-1) continue;
                        if(!visited[i][j]&&input.s[i][j]=='J') chmin(p,{dist[i][j],{i,j}});
                    }
                }
                if(p.first==1e9) break;
                int ti,tj;
                tie(ti,tj)=p.second;
                vector<pair<int,int>> new_v=v;
                new_v.emplace_back(ti,tj);
                result res=play(v_to_root(first_v,si,sj,new_v,gi,gj));
                if(res.hp<=min_hp){
                    if(first_flag) end_flag=true;
                    break;
                }
                v.emplace_back(ti,tj);
                ni=ti;
                nj=tj;
                visited=res.visited;
                first_flag=false;
            }
            if(end_flag) break;
            int n=v.size();
            if(n==0) break;
            while(true){
                bool reversed=false;
                rep(i,n-1){
                    for(int j=i+2;j<n-1;j++){
                        int x=dist_juwel[v[i].first][v[i].second][v[i+1].first][v[i+1].second];
                        int y=dist_juwel[v[j].first][v[j].second][v[j+1].first][v[j+1].second];
                        int a=dist_juwel[v[i].first][v[i].second][v[j].first][v[j].second];
                        int b=dist_juwel[v[i+1].first][v[i+1].second][v[j+1].first][v[j+1].second];
                        if(a+b<x+y){
                            reverse(v.begin()+i+1,v.begin()+j+1);
                            reversed=true;
                            break;
                        }
                    }
                    if(reversed) break;
                }
                if(!reversed) break;
            }
            //if(play(v_to_root(first_v,si,sj,new_v,gi,gj)).hp<=min_hp) break;
            ni=v.back().first;
            nj=v.back().second;
        }
        return v_to_v(first_v,si,sj,v,gi,gj);
    }
    void solve(){
        vector<vector<vector<vector<int>>>> dist_juwel(input.n,vector<vector<vector<int>>>(input.n,vector<vector<int>>(input.n,vector<int>(input.n,-1))));
        rep(i,input.n){
            rep(j,input.n){
                if(input.s[i][j]=='J') dist_juwel[i][j]=bfs(i,j);
            }
        }
        pair<int,vector<pair<int,int>>> ans={(int)-1e9,{}};
            vector<pair<int,int>> v;
            v={{input.si,input.sj},{input.ki,input.kj}};
            v=solve_root(v,input.ki,input.kj,input.gi,input.gj,0,dist_juwel);
            chmax(ans,{play(v_to_root(v)).score,v});
        for(auto &i:v_to_root(ans.second)){
            cout << "M " << i << '\n';
        }
    }
}
solver;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    input.input_cin();
    solver.solve();
}
0