結果
問題 | No.5015 Escape from Labyrinth |
ユーザー | FplusFplusF |
提出日時 | 2023-04-15 19:11:13 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,929 ms / 3,000 ms |
コード長 | 12,233 bytes |
コンパイル時間 | 3,629 ms |
コンパイル使用メモリ | 247,088 KB |
実行使用メモリ | 63,912 KB |
スコア | 124,910 |
最終ジャッジ日時 | 2023-04-15 19:16:00 |
合計ジャッジ時間 | 284,712 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge14 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2,509 ms
63,676 KB |
testcase_01 | AC | 2,858 ms
63,708 KB |
testcase_02 | AC | 2,806 ms
63,736 KB |
testcase_03 | AC | 2,765 ms
63,680 KB |
testcase_04 | AC | 2,749 ms
63,296 KB |
testcase_05 | AC | 2,769 ms
63,604 KB |
testcase_06 | AC | 2,855 ms
63,520 KB |
testcase_07 | AC | 2,776 ms
63,604 KB |
testcase_08 | AC | 2,769 ms
63,856 KB |
testcase_09 | AC | 2,736 ms
63,616 KB |
testcase_10 | AC | 2,313 ms
63,704 KB |
testcase_11 | AC | 2,730 ms
63,640 KB |
testcase_12 | AC | 2,165 ms
63,612 KB |
testcase_13 | AC | 2,854 ms
63,644 KB |
testcase_14 | AC | 2,898 ms
63,576 KB |
testcase_15 | AC | 2,723 ms
63,612 KB |
testcase_16 | AC | 2,856 ms
63,784 KB |
testcase_17 | AC | 2,901 ms
63,608 KB |
testcase_18 | AC | 2,191 ms
63,600 KB |
testcase_19 | AC | 2,799 ms
63,540 KB |
testcase_20 | AC | 2,297 ms
63,860 KB |
testcase_21 | AC | 2,261 ms
63,784 KB |
testcase_22 | AC | 2,845 ms
63,540 KB |
testcase_23 | AC | 2,135 ms
63,712 KB |
testcase_24 | AC | 2,736 ms
63,664 KB |
testcase_25 | AC | 2,750 ms
63,596 KB |
testcase_26 | AC | 2,850 ms
63,724 KB |
testcase_27 | AC | 2,872 ms
63,496 KB |
testcase_28 | AC | 2,147 ms
63,524 KB |
testcase_29 | AC | 2,794 ms
63,780 KB |
testcase_30 | AC | 2,780 ms
63,696 KB |
testcase_31 | AC | 2,859 ms
63,624 KB |
testcase_32 | AC | 2,812 ms
63,600 KB |
testcase_33 | AC | 2,782 ms
63,772 KB |
testcase_34 | AC | 2,300 ms
63,580 KB |
testcase_35 | AC | 2,844 ms
63,740 KB |
testcase_36 | AC | 2,730 ms
63,692 KB |
testcase_37 | AC | 2,860 ms
63,320 KB |
testcase_38 | AC | 2,814 ms
63,912 KB |
testcase_39 | AC | 2,084 ms
63,712 KB |
testcase_40 | AC | 2,282 ms
63,876 KB |
testcase_41 | AC | 2,514 ms
63,580 KB |
testcase_42 | AC | 2,855 ms
63,608 KB |
testcase_43 | AC | 2,773 ms
63,332 KB |
testcase_44 | AC | 1,698 ms
63,640 KB |
testcase_45 | AC | 2,793 ms
63,696 KB |
testcase_46 | AC | 2,917 ms
63,616 KB |
testcase_47 | AC | 2,860 ms
63,584 KB |
testcase_48 | AC | 2,782 ms
63,424 KB |
testcase_49 | AC | 2,731 ms
63,768 KB |
testcase_50 | AC | 2,725 ms
63,760 KB |
testcase_51 | AC | 2,873 ms
63,508 KB |
testcase_52 | AC | 2,845 ms
63,752 KB |
testcase_53 | AC | 2,768 ms
63,604 KB |
testcase_54 | AC | 2,730 ms
63,844 KB |
testcase_55 | AC | 2,769 ms
63,768 KB |
testcase_56 | AC | 2,864 ms
63,708 KB |
testcase_57 | AC | 2,858 ms
63,596 KB |
testcase_58 | AC | 2,739 ms
63,560 KB |
testcase_59 | AC | 2,109 ms
63,680 KB |
testcase_60 | AC | 2,802 ms
63,456 KB |
testcase_61 | AC | 2,870 ms
63,656 KB |
testcase_62 | AC | 2,829 ms
63,668 KB |
testcase_63 | AC | 2,764 ms
63,544 KB |
testcase_64 | AC | 2,721 ms
63,700 KB |
testcase_65 | AC | 2,294 ms
63,608 KB |
testcase_66 | AC | 2,876 ms
63,896 KB |
testcase_67 | AC | 2,848 ms
63,684 KB |
testcase_68 | AC | 2,812 ms
63,760 KB |
testcase_69 | AC | 2,783 ms
63,640 KB |
testcase_70 | AC | 2,766 ms
63,712 KB |
testcase_71 | AC | 2,852 ms
63,748 KB |
testcase_72 | AC | 2,185 ms
63,700 KB |
testcase_73 | AC | 2,816 ms
63,676 KB |
testcase_74 | AC | 2,915 ms
63,548 KB |
testcase_75 | AC | 2,929 ms
63,540 KB |
testcase_76 | AC | 1,606 ms
63,708 KB |
testcase_77 | AC | 2,831 ms
63,712 KB |
testcase_78 | AC | 2,783 ms
63,712 KB |
testcase_79 | AC | 2,740 ms
63,612 KB |
testcase_80 | AC | 2,756 ms
63,772 KB |
testcase_81 | AC | 2,910 ms
63,784 KB |
testcase_82 | AC | 2,785 ms
63,544 KB |
testcase_83 | AC | 2,765 ms
63,704 KB |
testcase_84 | AC | 2,805 ms
63,860 KB |
testcase_85 | AC | 2,924 ms
63,604 KB |
testcase_86 | AC | 2,907 ms
63,852 KB |
testcase_87 | AC | 2,837 ms
63,452 KB |
testcase_88 | AC | 2,850 ms
63,644 KB |
testcase_89 | AC | 2,919 ms
63,784 KB |
testcase_90 | AC | 2,764 ms
63,228 KB |
testcase_91 | AC | 2,860 ms
63,432 KB |
testcase_92 | AC | 2,774 ms
63,644 KB |
testcase_93 | AC | 2,849 ms
63,888 KB |
testcase_94 | AC | 2,721 ms
63,504 KB |
testcase_95 | AC | 2,789 ms
63,572 KB |
testcase_96 | AC | 2,921 ms
63,444 KB |
testcase_97 | AC | 2,477 ms
63,692 KB |
testcase_98 | AC | 2,770 ms
63,364 KB |
testcase_99 | AC | 2,740 ms
63,404 KB |
ソースコード
#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) return {(int)-1e9,(int)-1e9}; } 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}); } } } int ni=si,nj=sj; while(ni!=gi||nj!=gj){ 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){ ni=ti; nj=tj; ret.push_back(dir[k]); break; } } } return ret; } string solve_root(string first_root,int si,int sj,int gi,int gj,int min_hp,int time_limit,vector<vector<vector<vector<int>>>> &dist_juwel){ string root=first_root; vector<pair<int,int>> visit_juwel; int ni=si,nj=sj; while(true){ vector<vector<bool>> visited=play(root).visited; while(true){ auto now=chrono::system_clock::now(); int msec=chrono::duration_cast<chrono::milliseconds>(now-start).count(); if(time_limit<=msec) break; 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; string new_root=root; new_root+=min_path(ni,nj,ti,tj); string new_new_root=new_root+min_path(ti,tj,gi,gj); if(play(new_new_root).hp<=min_hp){ break; } visit_juwel.emplace_back(ti,tj); result res=play(new_root); ni=ti; nj=tj; root=new_root; visited=res.visited; } auto now_a=chrono::system_clock::now(); int msec_a=chrono::duration_cast<chrono::milliseconds>(now_a-start).count(); if(time_limit<=msec_a) break; auto now_start=chrono::system_clock::now(); int n=visit_juwel.size(); if(n==0) break; int pre_dist=dist_root(visit_juwel,dist_juwel); while(true){ auto now_now=chrono::system_clock::now(); int msec=chrono::duration_cast<chrono::milliseconds>(now_now-now_start).count(); if(200<=msec) break; int a=mt()%n,b=mt()%n; if(b<a) swap(a,b); reverse(visit_juwel.begin()+a,visit_juwel.begin()+b); int new_dist=dist_root(visit_juwel,dist_juwel); if(new_dist<pre_dist){ pre_dist=new_dist; }else{ reverse(visit_juwel.begin()+a,visit_juwel.begin()+b); } } string new_root=first_root; if((int)visit_juwel.size()!=0){ new_root+=min_path(si,sj,visit_juwel.front().first,visit_juwel.front().second); rep(i,n-1){ new_root+=min_path(visit_juwel[i].first,visit_juwel[i].second,visit_juwel[i+1].first,visit_juwel[i+1].second); } } string new_new_root=new_root; new_new_root+=min_path(visit_juwel.back().first,visit_juwel.back().second,gi,gj); if(play(new_new_root).hp<=min_hp) break; root=new_root; ni=visit_juwel.back().first; nj=visit_juwel.back().second; auto now=chrono::system_clock::now(); int msec=chrono::duration_cast<chrono::milliseconds>(now-start).count(); if(time_limit<=msec) break; } root+=min_path(ni,nj,gi,gj); return root; } 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,string> ans={(int)-1e9,""}; string root=""; root=solve_root(root,input.si,input.sj,input.ki,input.kj,input.h/2,500,dist_juwel); root=solve_root(root,input.ki,input.kj,input.gi,input.gj,0,1000,dist_juwel); chmax(ans,{play(root).score,root}); root=""; root=min_path(input.si,input.sj,input.ki,input.kj); root=solve_root(root,input.ki,input.kj,input.gi,input.gj,0,1800,dist_juwel); chmax(ans,{play(root).score,root}); string x=min_path(input.ki,input.kj,input.gi,input.gj); root=""; root=solve_root(root,input.si,input.sj,input.ki,input.kj,input.h-play_free(input.ki,input.kj,x).hp,2700,dist_juwel); root+=x; chmax(ans,{play(root).score,root}); for(auto &i:ans.second){ cout << "M " << i << '\n'; } } } solver; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); input.input_cin(); solver.solve(); }