数据:
Input
There are multiple test cases.
Each test case contains several lines.
The first line contains two integers N(1≤N≤100000) and M(0≤M≤1000).
Then M lines follow, each line contains two integers Xi,Yi(1≤Xi<Yi≤N).
The input end with N=0, M=0.Output
For each test case in the input, you should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.
Sample Input
2 0 8 3 2 4 4 5 7 8 0 0Sample Output
1.1667 2.3441
题意 原文+大意
Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). When Hzz is at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.
There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0<Xi<Yi<=N) without throwing the dice. If there is another flight line from Yi, Hzz can take the flight line continuously. It is granted that there is no two or more flight lines start from the same grid.Hzz非常喜欢飞机象棋。象棋地图包含从0到N的N+1个网格。Hzz从网格0开始。每走一步,他就掷出一个骰子(一个骰子有六个正面,正面的概率相等,正面的数字是1,2,3,4,5,6)。当Hzz在第一格,骰子数是x时,他将移动到第一格+x。当i+x等于或大于n时,Hzz结束游戏 国际象棋地图上也有M条飞行路线。第一条航线可以帮助Hzz从网格Xi飞到Yi(0<Xi <Yi< =N),而不用掷骰子。如果还有一条航线从易出发,Hzz可以继续走这条航线。允许没有两条或更多的航线从同一个网格开始。
求到n点的期望数;
思路: dp[i]表示在i点到N点的期望值,dp[N]=0,求dp[0],没有飞路线的情况下dp[i]=dp[i+1]/6+dp[i+2]/6+...+dp[i+6]/6+1,倒推求解即可。
CODE:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int N=1e5+10;
int f[N];
double dp[N];
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
if(n==0&&m==0)break;
mem(f,0),mem(dp,0);///多组数据,记得清空
int a,b;
for(int i=1; i<=m; i++)
{
scanf("%d %d",&a,&b);
f[a]=b;
}
dp[n]=0;
for(int i=n-1; i>=0; i--)
{
if(f[i]==0)
{
dp[i]=1;
for(int j=1; j<=6; j++)
dp[i]+=dp[i+j]/6;
}
else dp[i]=dp[f[i]];
}
printf("%.4f\n",dp[0]);
}
return 0;
}
期望dp~加油丫~