LightOJ 1151 Snakes and Ladders (期望DP + 高斯消元)

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 i94) : 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]=61k=1k6dp[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]=61k=1i+k100dp[i+k]+61k=101ki+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();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值