Snakes and Ladders LightOJ

'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

有待理解:

 1 #include<bits/stdc++.h>
 2 #define EPS 1e-8
 3 using namespace std;
 4 
 5 int x,kase;
 6 int a[110];
 7 double dp[110][110];
 8 
 9 void Gauss(int n,int m){
10     int col,row,mxr;
11     for(col=row=1;row<=n&&col<=m;row++,col++){
12         mxr=row;
13         for(int i=row+1;i<=n;i++) if(fabs(dp[i][col])>fabs(dp[mxr][col])) mxr=i;
14         if(mxr!=row) swap(a[row],a[mxr]);
15         if(fabs(dp[row][col])<EPS){ row--; continue; }
16         for(int i=1;i<=n;i++){
17             if(i!=row&&fabs(dp[i][col])>EPS){
18                 for(int j=m;j>=col;j--) dp[i][j]-=dp[row][j]/dp[row][col]*dp[i][col];        
19             }
20         } 
21     }
22     row--;
23     for(int i=row;i>=1;i--){
24         for(int j=i+1;j<=row;j++) dp[i][m]-=dp[j][m]*dp[i][j];
25         dp[i][m]/=dp[i][i];
26     }
27 }
28 
29 void Solve(){
30     for(int i=1;i<100;i++){
31         if(a[i]){
32             dp[i][a[i]]=-1;
33             dp[i][101]=0;
34             dp[i][i]=1;
35         }
36         else{
37             int cnt=0;
38             for(int j=1;i+j<=100&&j<=6;j++){
39                 cnt++;
40                 dp[i][i+j]=-1;
41             }
42             dp[i][i]=cnt;
43             dp[i][101]=6;
44         }
45     }
46     dp[100][100]=1;
47     dp[100][101]=0;
48     Gauss(100,101);
49 }
50 
51 int main()
52 {   cin>>kase;
53     for(int t=1;t<=kase;t++){
54         cin>>x;
55         memset(a,0,sizeof(a));
56         memset(dp,0,sizeof(dp));
57         for(int i=1;i<=x;i++){
58             int u,v;
59             cin>>u>>v;
60             a[u]=v;
61         }
62         Solve();
63         printf("Case %d: %lf
",t,dp[1][101]);
64     }
65     return 0;
66 } 
原文地址:https://www.cnblogs.com/zgglj-com/p/7806934.html