Snakes and Ladders LightOJ - 1151 (求期望 高斯消元)

'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,每次走的步数用一个大小为6的骰子决定。地图上有很多个通道 A可以直接到B,不过A和B大小不确定   而且 如果99扔到100 那么只有1能走 扔其他的都要再扔一次      问从1走到100的扔骰子个数的期望

解 :因为A可能比B大所以不能递推   要高斯消元

#include<bits/stdc++.h>
using namespace std;
#define eps 1e-9
const int maxn=110;
double a[maxn][maxn],x[maxn];//a为方程左边的矩阵   x为等式右边的值  ,求解之后 x存的就是值
int f[maxn];
int equ=100,var=100;//固定 100个方程 100个解
double fab(double k)求绝对值
{
    return k < 0 ? -k : k;
}
int Gauss()//高斯消元 返回 0 无解    返回 1有解
{
    int i,j,k,col,max_r;
    for(k=0,col=0;k<equ&&col<var;k++,col++)
    {
        max_r=k;
        for(i=k+1;i<equ;i++)
            if(fab(a[max_r][col])>fab(a[max_r][col]))
               max_r=i;
        if(fab(a[max_r][col])<eps) return 0;
        if(k!=max_r)
        {
            for(j=col;j<var;j++)
                swap(a[k][j],a[max_r][j]);
            swap(x[k],x[max_r]);
        }
        x[k]/=a[k][col];
        for(j=col+1;j<var;j++) a[k][j]/=a[k][col];
        a[k][col]=1;
        for(i=0;i<equ;i++)
        {
            if(i!=k)
            {
                x[i]-=x[k]*a[i][col];
                for(j=col+1;j<var;j++) a[i][j]-=a[k][j]*a[i][col];
                a[i][col]=0;
            }
        }
    }
    return 1;
}
void init()
{
    memset(f,0,sizeof(f));//是否有传送
    memset(a,0,sizeof(a));
    memset(x,0,sizeof(x));
}
int main()
{
 int t,n,a1,b1;
 scanf("%d",&t);
for(int oo=1;oo<=t;oo++)
 {
     scanf("%d",&n);
     init();
     while(n--)
     {
         scanf("%d%d",&a1,&b1);
         f[a1]=1;
         a[a1-1][a1-1]=1;
         a[a1-1][b1-1]=-1;
         x[a1-1]=0;
     }
     for(int i=1;i<100;i++)
     {
         if(f[i]) continue;
         if(i<=94)
         {
             a[i-1][i-1]=1;
             for(int j=1;j<=6;j++)
                a[i-1][i+j-1]=-1.0/6.0;
             x[i-1]=1;
         }
         else
         {
             a[i-1][i-1]=1.0-(i*1.0-94)/6.0;
             for(int j=i+1;j<=100;j++)
                a[i-1][j-1]=-1.0/6.0;
             x[i-1]=1;
         }
     }
     a[99][99]=1;
     x[99]=0;
     Gauss();
     double ans=x[0];
    printf("Case %d: %.8f\n",oo,ans);

 }
}
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值