結果

問題 No.506 限られたジャパリまん
ユーザー tnakao0123tnakao0123
提出日時 2017-04-22 01:33:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 1,809 bytes
コンパイル時間 569 ms
コンパイル使用メモリ 83,712 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-10 12:53:12
合計ジャッジ時間 2,012 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 82 ms
4,384 KB
testcase_05 AC 81 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 15 ms
4,380 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 37 ms
4,380 KB
testcase_12 AC 17 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 14 ms
4,380 KB
testcase_17 AC 8 ms
4,380 KB
testcase_18 AC 21 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 40 ms
4,380 KB
testcase_21 AC 39 ms
4,380 KB
testcase_22 AC 73 ms
4,384 KB
testcase_23 AC 20 ms
4,380 KB
testcase_24 AC 1 ms
4,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 506.cc: No.506 限られたジャパリまん - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_H = 32;
const int MAX_W = 32;
const int MAX_K = 15;
const int KBITS = 1 << MAX_K;
const int MOD = 1000000007;

/* typedef */

typedef long long ll;
typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int,int> pii;

/* global variables */

ll dp[MAX_H + 1][MAX_W + 1];
int xs[MAX_K], ys[MAX_K], bnums[KBITS];
string nms[MAX_K];

/* subroutines */

/* main */

int main() {
  int h, w, k, p;
  cin >> h >> w >> k >> p;
  int kbits = 1 << k;

  for (int i = 0; i < k; i++) cin >> xs[i] >> ys[i] >> nms[i];

  bnums[0] = 0;
  for (int bits = 1, msb = 1; bits < kbits; bits++) {
    if ((msb << 1) <= bits) msb <<= 1;
    bnums[bits] = bnums[bits ^ msb] + 1;
  }

  ll maxdp = -1;
  int maxbits = 0;

  for (int bits = 0; bits < kbits; bits++) {
    if (bnums[bits] > p) continue;

    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for (int i = 0, bi = 1; i < k; i++, bi <<= 1)
      if (! (bits & bi)) dp[xs[i]][ys[i]] = -1;

    for (int x = 0; x <= h; x++)
      for (int y = 0; y <= w; y++)
	if (dp[x][y] >= 0) {
	  if (x < h && dp[x + 1][y] >= 0) dp[x + 1][y] += dp[x][y];
	  if (y < w && dp[x][y + 1] >= 0) dp[x][y + 1] += dp[x][y];
	}

    if (maxdp < dp[h][w]) maxdp = dp[h][w], maxbits = bits;
  }

  printf("%lld\n", maxdp % MOD);
  for (int i = 0, bi = 1; i < k; i++, bi <<= 1)
    if (maxbits & bi) puts(nms[i].c_str());
  return 0;
}
0