qscqesze
Published on 2017-09-02 11:31 in 暂未分类 with qscqesze

# Codeforces Round #597 (Div. 2) E. Hyakugoku and Ladders 概率dp

Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple just playing "Cat's Cradle" so now she wants to try a different game — "Snakes and Ladders". Unfortunately, she already killed all the snakes, so there are only ladders left now.

The game is played on a 10×10 board as follows:

At the beginning of the game, the player is at the bottom left square.
The objective of the game is for the player to reach the Goal (the top left square) by following the path and climbing vertical ladders. Once the player reaches the Goal, the game ends.
The path is as follows: if a square is not the end of its row, it leads to the square next to it along the direction of its row; if a square is the end of its row, it leads to the square above it. The direction of a row is determined as follows: the direction of the bottom row is to the right; the direction of any other row is opposite the direction of the row below it. See Notes section for visualization of path.
During each turn, the player rolls a standard six-sided dice. Suppose that the number shown on the dice is 𝑟. If the Goal is less than 𝑟 squares away on the path, the player doesn't move (but the turn is performed). Otherwise, the player advances exactly 𝑟 squares along the path and then stops. If the player stops on a square with the bottom of a ladder, the player chooses whether or not to climb up that ladder. If she chooses not to climb, then she stays in that square for the beginning of the next turn.
The numbers on the faces of the dice are 1, 2, 3, 4, 5, and 6, with each number having the same probability of being shown.

it is possible for ladders to overlap, but the player cannot switch to the other ladder while in the middle of climbing the first one;
it is possible for ladders to go straight to the top row, but not any higher;
it is possible for two ladders to lead to the same tile;
it is possible for a ladder to lead to a tile that also has a ladder, but the player will not be able to use that second ladder if she uses the first one;
the player can only climb up ladders, not climb down.
Hyakugoku wants to finish the game as soon as possible. Thus, on each turn she chooses whether to climb the ladder or not optimally. Help her to determine the minimum expected number of turns the game will take.

## Input

Input will consist of ten lines. The 𝑖-th line will contain 10 non-negative integers ℎ𝑖1,ℎ𝑖2,…,ℎ𝑖10. If ℎ𝑖𝑗 is 0, then the tile at the 𝑖-th row and 𝑗-th column has no ladder. Otherwise, the ladder at that tile will have a height of ℎ𝑖𝑗, i.e. climbing it will lead to the tile ℎ𝑖𝑗 rows directly above. It is guaranteed that 0≤ℎ𝑖𝑗<𝑖. Also, the first number of the first line and the first number of the last line always contain 0, i.e. the Goal and the starting tile never have ladders.

## Output

Print only one line containing a single floating-point number — the minimum expected number of turns Hyakugoku can take to finish the game. Your answer will be considered correct if its absolute or relative error does not exceed 10−6.

## Examples

input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
output
33.0476190476
input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 4 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 4 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 4 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 9
output
20.2591405923
input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 6 6 6 6 6 6 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
output
15.9047592939

## Note

A visualization of the path and the board from example 2 is as follows:

The tile with an 'S' is the starting tile and the tile with an 'E' is the Goal.

For the first example, there are no ladders.

For the second example, the board looks like the one in the right part of the image (the ladders have been colored for clarity).

## 题解

dp[i]表示从i位置到达终点需要投多少次骰子。如果没有终点和梯子的限制，dp[i]=(dp[i-1]+dp[i-2]+...+dp[i-6])/6+1即可。

## 代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
double dp[maxn*maxn];
int idx[maxn][maxn];
int main(){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(i%2){
idx[i][j]=(i*10+9-j);
}else{
idx[i][j]=(i*10+j);
}
}
}
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
int x;scanf("%d",&x);
}
}
dp[0]=0;
for(int i=1;i<6;i++)
dp[i]=6;
for(int i=6;i<100;i++){
double sum = 0;
for(int step=1;step<=6;step++){
}