結果

問題 No.506 限られたジャパリまん
ユーザー rickythetarickytheta
提出日時 2017-04-21 22:50:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 676 ms / 2,000 ms
コード長 2,164 bytes
コンパイル時間 1,205 ms
コンパイル使用メモリ 150,852 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 12:51:18
合計ジャッジ時間 5,970 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 676 ms
4,376 KB
testcase_05 AC 674 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 112 ms
4,380 KB
testcase_09 AC 32 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 309 ms
4,380 KB
testcase_12 AC 136 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 87 ms
4,380 KB
testcase_17 AC 54 ms
4,376 KB
testcase_18 AC 166 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 326 ms
4,380 KB
testcase_21 AC 330 ms
4,380 KB
testcase_22 AC 631 ms
4,376 KB
testcase_23 AC 158 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

int h,w,k,p;
pii pts[16];
string name[16];

int mp[35][35];
ll dp[35][35];

int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
ll check(){
  if(mp[0][0] == -1)return 0;
  queue<pii> Q;
  Q.push(pii(0,0));
  mp[0][0] = 0;
  REP(i,h+1)REP(j,w+1)dp[i][j] = 0;
  dp[0][0] = 1;
  while(!Q.empty()){
    pii P = Q.front(); Q.pop();
    int i = P.first;
    int j = P.second;
    int d = mp[i][j];
    ll yo = dp[i][j];
    REP(a,4){
      int ni = i+dy[a];
      int nj = j+dx[a];
      if(ni<0 || nj<0 || ni>h || nj>w)continue;
      if(mp[ni][nj] == d+1){
        dp[ni][nj] += yo;
      }else if(mp[ni][nj] > d+1){
        mp[ni][nj] = d+1;
        dp[ni][nj] = yo;
        Q.push(pii(ni,nj));
      }
    }
  }
  return dp[h][w];
}

int main(){
  scanf("%d%d%d%d",&h,&w,&k,&p);
  REP(i,k){
    cin>>pts[i].first>>pts[i].second>>name[i];
  }
  int mindist = 252521;
  ll mx = 0;
  int mxmask = 0;
  REP(mask,1<<k){
    if(__builtin_popcount(mask)>p)continue;
    REP(i,h+1)REP(j,w+1)mp[i][j] = 252521;
    REP(i,k)if(!((mask>>i)&1)){
      mp[pts[i].first][pts[i].second] = -1;
    }
    ll val = check();
    if(mp[h][w] < mindist || (mp[h][w]==mindist && val>mx)){
      mindist = mp[h][w];
      mx = val;
      mxmask = mask;
    }
  }
  cout<<(mx%MOD)<<endl;
  REP(i,k)if((mxmask>>i)&1){
    cout<<name[i]<<endl;
  }
  return 0;
}
0