您的位置:首页 > 其它

[hihoCoder太阁最新面经算法竞赛8]题目2 : Dice Possibility(简单记忆化搜索)

2016-07-17 11:09 579 查看
时间限制:10000ms
单点时限:1000ms
内存限制:256MB


描述

What is possibility of rolling N dice and the sum of the numbers equals to M?


输入

Two integers N and M. (1 ≤ N ≤ 100, 1 ≤ M ≤ 600)


输出

Output the possibility in percentage with 2 decimal places.

样例输入
2 10


样例输出
8.33


题解:简单记忆化搜索

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m;
double tmp;
double dp[101][610];
double dfs(int pos,int last) {
if(dp[pos][last]!=-1) return dp[pos][last];
int s=max(last-pos*6,1);
int e=min(last-pos*1,6);
double ans=0;
for(int i=s;i<=e;i++) {
double tmp_ans=dfs(pos-1,last-i);
if(tmp_ans!=0) ans+=tmp_ans*tmp;
}
dp[pos][last]=ans;
return dp[pos][last];
}
int main()
{
cin>>n>>m;
tmp=1.0/6;
for(int i=0;i<n;i++) {
for(int j=1;j<=600;j++) {
dp[i][j]=-1;
}
}
for(int i=1;i<=6;i++) {
dp[0][i]=tmp;
}
dfs(n-1,m);
if(dp[n-1][m]==-1) printf("0.00\n");
else printf("%.2f\n",dp[n-1][m]*100);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hihocoder 算法 搜索