Snakes and Ladders LightOJ - 1151 概率dp + 高斯消元

题目链接


'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

题意:

求从第一格走到第100格的期望步数,

思路:

期望倒着推,概率顺着推。

所以这题的状态转移方程其实很好推,但是鶸表示还是有点难度,
dp[i] 表示从从 i 点开始到终点的 期望, 那么第i个格子没传送时:
dp[i]=16ki=1dp[i+j]+166i+j=100,j=k+1dp[i]+1,k6
可以推出:
kdp[i]6i+j=100,j=k+1dp[i]=6

第i个格子有传送:
dp[i] = dp[next[i]];

然后, 我们发现无论怎么样我们都不能递推的解决这个式子, 于是就开始使用高斯消元解决, 直接解出答案即可,

但是这个题, 我们的答案是小数, 所以我们应该用最适合解小数的高斯消元来解, 这里如果使用kuangbin这一类的高斯版子的可能来样例都不能过, 因为每次消系数的会求最小公倍数, 然后导致系数越来越大, 最后会爆 long long, 博主亲测.

代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 110;
typedef long long LL;
const double eps = 1e-7;

double a[maxn][maxn];
int nextt[maxn];
double x[maxn];

void Debug(){
    for(int i = 1; i <= 101; ++i){
        for(int j = 1; j <= 101; ++j)
            printf("%f ", a[i][j]);
        printf("\n");
    }
}

double guass(){
    int var = 101, equ = 101;
    int k, col;
    for(k = 1, col = 1; k < equ && col < var; ++col, ++k){
        int maxr = k;
        for(int i = k + 1; i < equ; ++i)
            if(abs(a[maxr][col]) < abs(a[i][col]))
                maxr = i;
        if(maxr != k)
            for(LL i = col; i <= var; ++i)
                swap(a[maxr][i], a[k][i]);
        if(fabs(a[k][col]) < eps){
            k--;
            continue;
        }for(int i = k + 1; i < equ; ++i)
            if(fabs(a[i][col]) >eps){
              double t1 = a[i][col]/a[k][col];
              for(int j = col; j <= var; ++j)
                a[i][j] -= a[k][j] * t1;
            }
    }for(int i = var - 1; i >= 1; --i){
        double tmp = a[i][var] * 1.0;
        for(int j = i + 1; j < var; ++j)
            tmp -= x[j] * a[i][j];
        x[i] = tmp/a[i][i] ;
    }return x[1];//第一个未知数的解就是答案
}

int main(){
    int t, kase = 0, n;
    scanf("%d",&t);
    while(t--){
        scanf("%d", &n);
        memset(nextt, 0, sizeof(nextt));
        while(n--){
            int x, y;
            scanf("%d %d",&x, &y);
            nextt[x] = y;
        }memset(a, 0, sizeof(a));
        memset(x, 0, sizeof(x));
        for(int i = 1; i < 100; ++i) //构造方程组, 根据刚才推出的递推式.
            if(nextt[i]){
                    a[i][i] = 1;
                    a[i][nextt[i]] = -1;
                    a[i][101] = 0;
            }else {
                int cnt = 0;
                a[i][101] = 6;
                for(int j = 1; i + j <= 100 && j <= 6; ++j){
                    ++cnt;
                    a[i][i+j] = -1;
                }a[i][i] = cnt;
            }a[100][100] = 1;
        printf("Case %d: %.7f\n", ++kase, guass());
    }return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值