結果
問題 |
No.506 限られたジャパリまん
|
ユーザー |
![]() |
提出日時 | 2017-04-21 23:07:10 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,480 bytes |
コンパイル時間 | 1,460 ms |
コンパイル使用メモリ | 172,272 KB |
実行使用メモリ | 31,232 KB |
最終ジャッジ日時 | 2024-06-28 04:11:58 |
合計ジャッジ時間 | 5,366 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 4 TLE * 1 -- * 20 |
ソースコード
#include<bits/stdc++.h> using namespace std; #define int long long int MOD=1000000007LL; int ans,len,bit; int h,w,k,p; int x[20],y[20]; string n[20]; int dp[40][40][1600]; bool used[40][40][1600]; typedef pair<int,int> P; struct st{ int y,x,d; st(){} st(int y,int x,int d):y(y),x(x),d(d){} }; signed main(){ cin>>h>>w>>k>>p; for(int i=0;i<k;i++) cin>>x[i]>>y[i]>>n[i]; map<P,int> m; for(int i=0;i<k;i++) m[P(y[i],x[i])]=i; int ax[4]={1,-1,0,0}; int ay[4]={0,0,1,-1}; ans=0;len=-1;bit=0; for(int b=0;b<(1<<k);b++){ if(__builtin_popcount(b)>p) continue; memset(dp,0,sizeof(dp)); memset(used,0,sizeof(used)); queue<st> q; dp[0][0][0]=1; used[0][0][0]=1; q.push(st(0,0,0)); int tmp=-1; while(!q.empty()){ st s=q.front();q.pop(); int cy=s.y,cx=s.x,cd=s.d; if(~len&&len<cd) break; if(cy==w&&cx==h){ tmp=cd; break; } for(int l=0;l<4;l++){ int ny=cy+ay[l],nx=cx+ax[l],nd=cd+1; if(ny<0||ny>w||nx<0||nx>h) continue; if(m.count(P(ny,nx))&&((b>>m[P(ny,nx)])&1)==0) continue; dp[ny][nx][nd]+=dp[cy][cx][cd]; if(!used[ny][nx][nd]){ used[ny][nx][nd]=1; q.push(st(ny,nx,nd)); } } } if(tmp<0) continue; if(len<0||tmp<len){ len=tmp; ans=dp[w][h][tmp]; bit=b; } if(len==tmp&&ans<dp[w][h][tmp]){ ans=dp[w][h][tmp]; bit=b; } } cout<<ans%MOD<<endl; for(int i=0;i<k;i++) if((bit>>i)&1) cout<<n[i]<<endl; return 0; }