LightOJ - 1151 Snakes and Ladders —— 期望、高斯消元法

题目链接:https://vjudge.net/problem/LightOJ-1151

 

1151 - Snakes and Ladders
    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

'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

Output for 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

Case 1: 31.54880806

Case 2: 33.0476190476

 题意:

有100个格子,从1开始走,每次抛骰子走1~6,若抛出的点数导致走出了100以外,则重新抛一次。有n个格子会单向传送到其他格子,tp[i]表示从i传送到tp[i]。1和100不会有传送,一个格子也不会有两种传送。问走到100的所抛骰子次数的期望值。

 

题解:

 

 

代码如下

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const double eps = 1e-9;
17 const int MOD = 1e9+7;
18 const int MAXN = 100+10;
19 
20 double a[MAXN][MAXN], x[MAXN];
21 int Gauss(int equ, int var)
22 {
23     int i, j, k, col, max_r;
24     for(k = 1,col = 1; k<=equ&&col<=var; k++,col++)
25     {
26         max_r = k;
27         for(i = k+1; i<=equ; i++)
28             if(fabs(a[i][col])>fabs(a[max_r][col]))
29                 max_r = i;
30         if(fabs(a[max_r][col])<eps) return 0;
31         if(k!=max_r)
32         {
33             for(j = col; j<=var; j++)
34                 swap(a[k][j], a[max_r][j]);
35             swap(x[k], x[max_r]);
36         }
37         x[k] /= a[k][col];
38         for(j = col+1; j<=var; j++) a[k][j] /= a[k][col];
39         a[k][col] = 1;
40         for(i = 1; i<=equ; i++)
41             if(i!=k)
42             {
43                 x[i] -= x[k]*a[i][k];
44                 for(j = col+1; j<var; j++) a[i][j] -= a[k][j]*a[i][col];
45                 a[i][col] = 0;
46             }
47     }
48     return 1;
49 }
50 
51 int nxt[MAXN];
52 int main()
53 {
54     int T, kase = 0;
55     scanf("%d", &T);
56     while(T--)
57     {
58         int n;
59         scanf("%d", &n);
60         memset(nxt, 0, sizeof(nxt));
61         for(int i = 1; i<=n; i++)
62         {
63             int u, v;
64             scanf("%d%d", &u,&v);
65             nxt[u] = v;
66         }
67 
68         memset(a, 0, sizeof(a));
69         memset(x, 0, sizeof(x));
70         for(int i = 1; i<100; i++)
71         {
72             if(nxt[i])
73             {
74                 a[i][i] = 1;
75                 a[i][nxt[i]] = -1;
76                 x[i] = 0;
77             }
78             else
79             {
80                 int cnt = 0;
81                 for(int j = 1; i+j<=100&&j<=6; j++)
82                 {
83                     cnt++;
84                     a[i][i+j] = -1;
85                 }
86                 a[i][i] = cnt; x[i] = 6;
87             }
88         }
89         a[100][100] = 1; x[100] = 0;
90         Gauss(100,100);
91         printf("Case %d: %.10lf\n", ++kase, x[1]);
92     }
93 }
View Code

 

 

posted on 2018-02-13 20:35  h_z_cong  阅读(384)  评论(0编辑  收藏  举报

导航