結果

問題 No.506 限られたジャパリまん
ユーザー beetbeet
提出日時 2017-04-21 23:02:23
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,529 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 158,616 KB
実行使用メモリ 30,224 KB
最終ジャッジ日時 2023-09-10 12:51:38
合計ジャッジ時間 5,339 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
25,908 KB
testcase_01 AC 106 ms
25,824 KB
testcase_02 AC 193 ms
25,980 KB
testcase_03 AC 12 ms
25,900 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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++){
    int t=0;
    for(int j=0;j<k;j++) t+=(b>>j)&1;
    if(t>p) continue;
    memset(dp,0,sizeof(dp));
    memset(used,0,sizeof(used));
    queue<st> q;
    dp[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(used[cy][cx][cd]) continue;
      used[cy][cx][cd]=1;
      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];
	q.push(st(ny,nx,nd));
      }
    }
    //cout<<tmp<<endl;
    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;
  //cout<<bit<<endl;
  if(bit){
    for(int i=0;i<k;i++)
      if((bit>>i)&1) cout<<n[i]<<endl;
  }
  return 0;
}
0