hdu 1398 记忆化搜索

题目简单,直接贴代码

/*
* hdu1398/win.cpp
* Created on: 2011-10-23
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MAXV = 310;
const int MAXK = 17;
const int credits[] = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169,
196, 225, 256, 289 };
int dp[MAXV][MAXK];

int dfs(int v, int k) {
if (dp[v][k] == -1) {
int ret = 0;
if (k == 1) {
ret = 1;
} else {
int i = 0;
while (v >= i * credits[k - 1]) {
ret += dfs(v - i * credits[k - 1], k - 1);
i++;
}
}
dp[v][k] = ret;
}
return dp[v][k];
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int v;
memset(dp, -1, sizeof(dp));
while (scanf("%d", &v) == 1 && v > 0) {
printf("%d\n", dfs(v, MAXK));
}
return 0;
}

 

posted @ 2011-10-23 16:57  moonbay  阅读(171)  评论(0编辑  收藏  举报