【BestCoder】 HDOJ 5234 Happy birthday

1858 단어 dp
간단하다
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 105
#define maxm 40005
#define eps 1e-12
#define mod 998244353
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#pragma comment(linker, "/STACK:102400000,102400000")
#define pii pair<int, int> 
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

int g[maxn][maxn];
bool dp[maxn][maxn][maxn];
int n, m, kk;

void work()
{
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			scanf("%d", &g[i][j]);
	memset(dp, 0, sizeof dp);
	for(int i = 0; i <= n; i++)
		for(int j = 0; j <= m; j++)
			dp[i][j][0] = 1;
	for(int i = 1; i <= n; i++)
		for(int j = 1;  j <= m; j++) {
			int t = g[i][j];
			for(int k = kk; k >= t; k--) {
				dp[i][j][k] |= dp[i-1][j][k];
				dp[i][j][k] |= dp[i][j-1][k];
				dp[i][j][k] |= dp[i-1][j][k - t];
				dp[i][j][k] |= dp[i][j-1][k - t];
			}
		}
	int ans = 0;
	for(int i = kk; i >= 0; i--) {
		if(dp[n][m][i]) {
			ans = i;
			break;
		}
	}

	printf("%d
", ans); } int main() { while(scanf("%d%d%d", &n, &m, &kk)!=EOF) { work(); } return 0; }

좋은 웹페이지 즐겨찾기