题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=4405
题意
0-n的n+1个点,初始0,每一次会扔骰子,1-6面等概率,扔到几走几步。同时有些地方有飞机道,走飞机道可以直飞(可以连续飞,确保只往后飞),问期望投骰子数
思路
首先题意似乎有些问题?好像如果有飞机道就一定要走,附一个我刚发的discuss http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=42005&messageid=1&deep=0
题很裸,一维DP,从终点向起点递推,飞机道就直接飞,否则对后面六个可达地区dp值取平均再加1,输出dp[0]即可
代码
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
//#define int long long
//#define double long double
using namespace std;
typedef long long ll;
const int maxn=100010;
const int inf=0x3f3f3f3f;
int n,m,k;
double dp[maxn];
int tp[maxn];
signed main(){
IOS
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
int tn=1;
while(cin>>n>>m){
if(!n&&!m) break;
for(int i=0;i<=n+6;i++){
dp[i]=0;tp[i]=0;
}
while(m--){
int u,v;
cin>>u>>v;
tp[u]=v;
}
for(int i=n-1;~i;i--){
if(tp[i]){
dp[i]=dp[tp[i]];
continue;
}
for(int j=1;j<=6;j++)
dp[i]+=dp[i+j];
dp[i]/=6.0;
dp[i]+=1;
}
cout<<fixed<<setprecision(4)<<dp[0]<<endl;
}
return 0;
}