結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 334 ms
4,376 KB
testcase_05 AC 334 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 58 ms
4,376 KB
testcase_09 AC 20 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 154 ms
4,380 KB
testcase_12 AC 68 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 47 ms
4,376 KB
testcase_17 AC 27 ms
4,376 KB
testcase_18 AC 82 ms
4,376 KB
testcase_19 AC 6 ms
4,376 KB
testcase_20 AC 161 ms
4,376 KB
testcase_21 AC 161 ms
4,380 KB
testcase_22 AC 310 ms
4,376 KB
testcase_23 AC 78 ms
4,380 KB
testcase_24 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;

#define MOD 1000000007

int h;
int w;
int k;
int p;


struct st{
	int x;
	int y;
	string name;
};

vector<st> v;
char buf[20];

bool ng[40][40];

long double dp[40][40];

vector<string> ans;

int main(){
	cin >> h >> w >> k >> p;
	for (int i = 0; i < k; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		scanf("%s", buf);
		v.push_back({ a, b, buf });
	}
	int id = 0;
	long double val = 0;
	for (int i = 0; i < (1 << k); i++){
		int give = 0;
		memset(ng, false, sizeof(ng));
		for (int j = 0; j < k; j++){
			if ((i >> j) & 1){
				give++;
			}
			else{
				ng[v[j].x][v[j].y] = true;
			}
		}
		if (give > p)continue;
		for (int i = 0; i <= h; i++){
			for (int j = 0; j <= w; j++){
				dp[i][j] = 0;
			}
		}
		dp[0][0] = 1;
		for (int i = 0; i <= h; i++){
			for (int j = 0; j <= w; j++){
				if (!ng[i + 1][j]){
					dp[i + 1][j] += dp[i][j];
				}
				if (!ng[i][j + 1]){
					dp[i][j+1] += dp[i][j];
				}
			}
		}
		if (val < dp[h][w]){
			val = dp[h][w];
			id = i;
		}
	}
	int i = id;
	memset(ng, false, sizeof(ng));
	for (int i = 0; i <= h; i++){
		for (int j = 0; j <= w; j++){
			dp[i][j] = 0;
		}
	}
	for (int j = 0; j < k; j++){
		if ((i >> j) & 1){
			ans.push_back(v[j].name);
		}
		else{
			ng[v[j].x][v[j].y] = true;
		}
	}
	dp[0][0] = 1;
	for (int i = 0; i <= h; i++){
		for (int j = 0; j <= w; j++){
			dp[i][j] = (long long int)(dp[i][j]) % MOD;
			if (!ng[i + 1][j]){
				dp[i + 1][j] += dp[i][j];
			}
			if (!ng[i][j + 1]){
				dp[i][j + 1] += dp[i][j];
			}
		}
	}
	printf("%lld\n", (long long int)dp[h][w]);
	for (int i = 0; i < ans.size(); i++){
		printf("%s", ans[i].c_str());
		puts("");
	}
	return 0;
}
0