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

大致题意:一张10*10的格子地图,编号1到100,起点为1,终点为100,每次甩一次标准的骰子,所走步数即所得点数,如果超过100,则重新甩一次,地图上有若干个相对应的传送点,比如a->b,表示你走到a格子的时候就直接传送到b,一个格子不会有两种传送方式,问走到终点的甩骰子的期望次数是多少。

思路:假设E(i)表示i号点到终点的期望次数。如果i号点是传送点,传送到点j,那么E(i)=E(j);
如果不是则 E(i)=1/6*E(i+1)+1/6*E(i+2)..+1/6*E(i+j)+(6-j)/6*E(i),i+j<=100&&j<=6。
然后建立矩阵,用高斯消元来搞即可

代码如下

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
#define LL long long 
#define ULL unsigned long long 
const int maxn=105;
const double eps=1e-12;  
int vis[105];//判断当前位置是否会传送到别的位置上 
double a[maxn][maxn];//每个方程中的未知数的系数
double ans[maxn];//解集

int equ,var;//equ个方程,var个变量 
bool free_x[maxn]; 
int sgn(double x)  
{  
    return (x>eps)-(x<-eps);  
}  

int gauss()
{
    equ=100,var=100;  
    int i,j,k;  
    int max_r; // 当前这列绝对值最大的行.  
    int col; // 当前处理的列.  
    double temp;  
    // 转换为阶梯阵.  
    col=0; // 当前处理的列.  
    memset(free_x,true,sizeof(free_x));  
    for(k=0;k<equ&&col<var;k++,col++)  
    {  
        max_r=k;  
        for(i=k+1;i<equ;i++) //找出当前这列绝对值最大的行
        {  
            if(sgn(fabs(a[i][col])-fabs(a[max_r][col]))>0)  
                max_r=i;  
        }  
        if(max_r!=k)  
        { // 与第k行交换.  
            for(j=k;j<var+1;j++)  
                swap(a[k][j],a[max_r][j]);  
        }  
        if(sgn(a[k][col])==0)  
        { // 说明该col列第k行以下全是0了,则处理当前行的下一列.  
            k--; continue;  
        }  
        for(i=k+1;i<equ;i++)  
        { // 枚举要删去的行,即将第k行以下的第col个变量的系数都变为0  
            if (sgn(a[i][col])!=0)  
            {  
                temp=a[i][col]/a[k][col];  
                for(j=col;j<var+1;j++)  
                {  
                    a[i][j]=a[i][j]-a[k][j]*temp;  
                }  
            }  
        }  
    }  

    for(i=k;i<equ;i++)//此时开始的行的所有变量前面的系数都为0,方程结果应该也为0  
    {   
        if (sgn(a[i][col])!=0)  //如果不为0则说明无解 
            return 0;  
    }  

    if(k<var)//如果存在自由元,自由元个数为var-k   
    {  
        for(i=k-1;i>=0;i--)  
        {  
            int free_x_num=0;  //自由变元的个数
            int free_index;  //自由变元的序号  

            for(j=0;j<var;j++)  
            {  
                if (sgn(a[i][j])!=0&&free_x[j])  
                    free_x_num++,free_index=j;  
            }  
            if(free_x_num>1) continue;  //该行中的不确定的变元的个数超过1个,无法求解,它们仍然为不确定的变元  
            //只有一个不确定的变元free_index,可以求解出该变元,且该变元是确定的
            temp=a[i][var];  
            for(j=0;j<var;j++)  
            {  
                if(sgn(a[i][j])!=0&&j!=free_index)  
                    temp-=a[i][j]*ans[j];  
            }  
            ans[free_index]=temp/a[i][free_index];  
            free_x[free_index]=0;  
        }  
        return var-k;  //返回自由元个数 
    }  

    for (i=var-1;i>=0;i--) //不存在自由元的话 
    {  
        temp=a[i][var];  
        for(j=i+1;j<var;j++)  
        {  
            if(sgn(a[i][j])!=0)  
                temp-=a[i][j]*ans[j];  
        }  
        ans[i]=temp/a[i][i];  
    }  
    return 1;  
}
int main()
{
    int T;
    scanf("%d",&T); 
    for(int cas=1;cas<=T;cas++)
    {
        memset(vis,0,sizeof(vis));
        memset(a,0,sizeof(a));
        int m;
        scanf("%d",&m);
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            vis[x-1]=1;
            a[x-1][x-1]=1.0;
            a[x-1][y-1]=-1.0;
            a[x-1][100]=0;
        }

        for(int i=0;i<=99;i++)
        {
            if(vis[i]==0)
            {
                if(i+6<=99)
                {
                    a[i][i]=1;
                    for(int j=i+1;j<=i+6;j++)
                    a[i][j]=-(1.0/6.0);
                }
                else 
                {
                    a[i][i]=(99-i)*1.0/6;
                    int num=99-i;
                    for(int j=i+1;j<=i+num;j++)
                    a[i][j]=-(1.0/6.0);
                }
                a[i][100]=1.0;
            }
        }
        a[99][99]=1;a[99][100]=0;
        gauss();
        printf("Case %d: %.8f\n",cas,ans[0]);

    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值