Description
‘Snakes and Ladders’ or ‘Shap-Ludu’ is a game commonly played in Bangladesh. The game is so common that it would be tough to find a person who hasn’t played it. But those who haven’t played it (unlucky of course!) the rules are as follows.
1.There is a 10 x 10 board containing some cells numbered from 1 to 100.
2.You start at position 1.
3.Each time you throw a perfect dice containing numbers 1 to 6.
4.There are some snakes and some ladders in the board. Ladders will take you up from one cell to another. Snakes will take you down.
5.If you reach a cell that contains the bottom part of a ladder, you will immediately move to the cell which contains the upper side of that ladder. Similarly if you reach a cell that has a snake-head you immediately go down to the cell where the tail of that snake ends.
6.The board is designed so that from any cell you can jump at most once. (For example there is a snake from 62 to 19, assume that another is from 19 to 2. So, if you reach 62, you will first jump to 19, you will jump to 2. These kinds of cases will not be given)
7.There is no snake head in the 100-th cell and no ladder (bottom part) in the first cell.
8.If you reach cell 100, the game ends. But if you have to go outside the board in any time your move will be lost. That means you will not take that move and you have to throw the dice again.
Now given a board, you have to find the expected number of times you need to throw the dice to win the game. The cases will be given such that a result will be found.
Input
Input starts with an integer T (≤ 105), denoting the number of test cases.
The first line of a case is a blank line. The next line gives you an integer n denoting the number of snakes and ladders. Each of the next n lines contain two integers a and b (1 ≤ a, b ≤ 100, a ≠ b). If a < b, it means that there is a ladder which takes you from a to b. If a > b, it means that there is a snake which takes you from a to b. Assume that the given board follows the above restrictions.
Output
For each case of input, print the case number and the expected number of times you need to throw the dice. Errors less than 10-6 will be ignored.
Sample Input
2
14
4 42
9 30
16 8
14 77
32 12
37 58
47 26
48 73
62 19
70 89
71 67
80 98
87 24
96 76
0
Sample Output
Case 1: 31.54880806
Case 2: 33.0476190476
Main idea & Solution
给一个10 * 10 的矩阵,依次编号为1 ~ 100 , 你从1出发,要去终点100。
你只能通过两种方法转移,一种是掷骰子,掷到几就走几步;另一种是利用传送门,直接传送到另一个位置
现求从起点到终点需要掷骰子的期望次数
特别的,如果当前掷骰子后应该到的位置会超出100的边界,那这次作废,需要继续掷。
设
d
p
[
i
]
=
dp[i] =
dp[i]= 从点
i
i
i 到 100 的期望次数,
① 有传送门:
d
p
[
i
]
=
d
p
[
t
o
[
i
]
]
dp\left[ i\right] =dp\left[ to\left[ i\right] \right]
dp[i]=dp[to[i]]
② 如果当前不可能会出界(
i
≤
94
i \leq 94
i≤94) :
d
p
[
i
]
=
1
6
∑
k
=
1
k
≤
6
d
p
[
i
+
k
]
+
1
dp\left[ i\right] =\dfrac {1}{6}\sum ^{k\leq 6}_{k=1}dp\left[ i+k\right] +1
dp[i]=61∑k=1k≤6dp[i+k]+1
③ 当前可能出界:
d
p
[
i
]
=
1
6
∑
k
=
1
i
+
k
≤
100
d
p
[
i
+
k
]
+
1
6
∑
k
=
101
k
≤
i
+
6
d
p
[
i
]
+
1
dp\left[ i\right] =\dfrac {1}{6}\sum ^{i+k\leq 100}_{k=1}dp\left[ i+k\right] +\dfrac {1}{6}\sum ^{k\leq i+6}_{k=101}dp\left[ i\right] +1
dp[i]=61∑k=1i+k≤100dp[i+k]+61∑k=101k≤i+6dp[i]+1
因为有传送门,所以存在环状转移,故需要高斯消元
若某次将要出界,其实就是留在原地不动,本质上就是有1/6概率从自己的状态转移过来
Code
#include <bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const double eps = 1e-6;
int n = 100;
double m[110][110];
bool tp[110];
void build(){
for(int i = 1;i < n;++i){
if(tp[i]) continue;
for(int k = 1;k <= 6;++k){
if(k + i <= 100) m[i][i+k] = (-1.0 / 6);
else m[i][i] -= 1.0 / 6;
}
m[i][i] += 1.0, m[i][n+1] = 1.0;
}
m[100][100] = 1.0, m[100][101] = 0.0;
}
double ans[110];
void gauss(){
for(int i = 1;i <= n;++i){
int r = i;
for(int j = i + 1;j <= n;++j){
if(fabs(m[r][i]) < fabs(m[j][i])) r = j;
}
if(i != r) swap(m[i],m[r]);
double div = m[i][i];
for(int j = i;j <= n + 1;++j) m[i][j] /= div;
for(int j = i + 1;j <= n;++j){
div = m[j][i];
for(int k = i;k <= n + 1;++k){
m[j][k] -= m[i][k] * div;
}
}
}
ans[n] = m[n][n+1];
for(int i = n - 1;i >= 1;--i){
ans[i] = m[i][n+1];
for(int j = i + 1;j <= n;++j)
ans[i] -= (m[i][j] * ans[j]);
}
}
void init(){
clr(m,0);clr(ans,0);clr(tp,false);
}
int main(){
int T,cas = 0;;scanf("%d",&T);
while(T--){
int q;scanf("%d",&q);
for(int i = 1;i <= q;++i){
int u,v;scanf("%d%d",&u,&v);tp[u] = true;
m[u][u] = 1.0, m[u][v] = -1.0;
}
build();gauss();
printf("Case %d: %.12f\n", ++cas,ans[1]);init();
}
}