码迷,mamicode.com
首页 > 其他好文 > 详细

[lightoj P1151] Snakes and Ladders

时间:2017-10-18 20:11:29      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:less   end   memory   ade   高斯消元   1.5   splay   move   else   

1151 - Snakes and Ladders

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. There is a 10 x 10 board containing some cells numbered from 1 to 100. 

  1. You start at position 1.
  2. Each time you throw a perfect dice containing numbers 1 to 6.
  3. 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.
  4. 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.
  5. 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)
  6. There is no snake head in the 100-th cell and no ladder (bottom part) in the first cell.
  7. 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

Output for Sample Input

Case 1: 31.54880806

Case 2: 33.0476190476

 

主要题意就不解释了。。

我们设从点i到100的步数期望为Ei。

则:

如果Ei有连向其他格子的边,设走到to[i],则Ei=Etoi。

否则Ei=(Ex1+Ex2+...+Exk)*(1/6)+1。其中,k=min(6,100-i),x1+1=x2,x2+1=x3,......xi+1=xi+1。

但是我们发现,to[i]可能大于i,也可能小于i,所以不能直接DP或递推。

所以相当于解一个有100个100元方程的方程组。其中最后一个方程已经确定,且得到E[100]=0。

那么,就相当于用高斯消元解一个有唯一解的实数方程组了。

code:

技术分享
 1 #include<bits/stdc++.h>
 2 #define Ms(a,x) memset(a,x,sizeof a)
 3 using namespace std;
 4 const int N=105;
 5 int n,got[N]; double a[N][N],E[N];
 6 double abso(double x) {return x>0?x:-x;}
 7 void Gauss(int equ,int var) {
 8     int row=1,col=1,cho;
 9     for (; row<=equ&&col<=var; row++,col++) {
10         cho=row;
11         for (int i=row+1; i<=equ; i++)
12             if (abso(a[i][col])>abso(a[cho][col])) cho=col;
13         if (cho!=row)
14             for (int i=col; i<=var+1; i++) swap(a[cho][i],a[row][i]);
15         if (abso(a[row][cho])<1e-6) {col--; continue;}
16         for (int i=row+1; i<=equ; i++) if (abso(a[i][col])>1e-10) {
17             double k=a[i][col]/a[row][col];
18             for (int j=col; j<=var+1; j++) a[i][j]-=k*a[row][j];
19         }
20     }
21     for (int i=var; i; i--) {
22         double re=a[i][var+1];
23         for (int j=i+1; j<=var; j++) re-=a[i][j]*E[j];
24         E[i]=re/a[i][i];
25     }
26 }
27 int main() {
28     int T; scanf("%d",&T);
29     for (int ts=1; ts<=T; ts++) {
30         cin>>n,Ms(got,0),Ms(a,0),Ms(E,0);
31         for (int i=1,x,y; i<=n; i++)
32             scanf("%d%d",&x,&y),got[x]=y;
33         for (int i=1,c; i<100; i++) if (!got[i]) {
34             c=min(6,100-i),a[i][i]=c,a[i][101]=6;
35             for (int j=1; j<=6&&i+j<=100; j++) a[i][i+j]=-1;
36         } else a[i][i]=1,a[i][got[i]]=-1,a[i][101]=0;
37         a[100][100]=1,a[100][101]=0;
38         Gauss(100,100);
39         printf("Case %d: %.10lf\n",ts,E[1]);
40     }
41     return 0;
42 }
View Code

 

[lightoj P1151] Snakes and Ladders

标签:less   end   memory   ade   高斯消元   1.5   splay   move   else   

原文地址:http://www.cnblogs.com/whc200305/p/7688061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!