Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7623 Accepted Submission(s): 4768
Problem Description
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.
Please help Hzz calculate the expected dice throwing times to finish the game.
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 0
Sample Output
1.1667
2.3441
题意:
有编号为0-n的n+1个网格,每次扔骰子,骰子有6个面,分别标有1,2,3,4,5,6.每一面的概率都是1/6.根据骰子的点数从0开始向前走,知道走到大于等于n结束。同时网格上有些格子会直接跳到下个格子,此时不需要扔骰子。问结束游戏需要仍多少次骰子。
令dp[i]表示在编号为i的网格中,还需要仍几次骰子就能结束游戏。那么dp[n]=0、dp[n+1]=0、dp[n+2]=0、dp[n+3]=0、dp[n+4]=0、dp[n+5]=0。(这些都是可能的末状态),此时已经结束游戏了,从后往前推。
对dp[i]来说:
1.如果i是可以跳跃的格子,那么dp[i]=dp[j](j是从i跳落的网格)
2.仍骰子,可能是跳1步、2步、3步、4步、5步、6步,
dp[i]=1/6(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])+1
最后加1是指这次扔了一次骰子
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<string.h>
#include<sstream>
#define ll long long
using namespace std;
const int N = 1e5+10;
const double INF = 0x3f3f3f3f;
double dp[N];
int s[N];
int main()
{
int n,m;
while(1)
{
scanf("%d%d",&n,&m);
memset(s,0,sizeof(s));
if(n==0&&m==0) break;
int x,y;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
s[x]=y;
}
for(int i=0;i<=5;i++)
{
dp[n+i]=0;
}
for(int i=n-1;i>=0;i--)
{
double z=1.0/6.0;
if(s[i]!=0)
{
dp[i]=dp[s[i]];
}
else
{
dp[i]=z*(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])+1;
}
}
printf("%.4f\n",dp[0]);
}
return 0;
}