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

有待理解:

 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\n",t,dp[1][101]);
64     }
65     return 0;
66 } 
时间: 2024-10-30 03:55:37

Snakes and Ladders LightOJ - 1151的相关文章

LightOJ 1151 Snakes and Ladders 高斯消元

给你一个地图10*10,从1到100,问掷骰子的次数的期望,中间会有传送门从a到b. 高斯消元基础题,学了一发板子,x存放多出来的,a存放系数: 要么是dp[i]=(dp[i+1]+...+dp[i+6]+6)/6; 要么是dp[i]=dp[go[i]]; 值得注意的是哪怕是没有dp[i+6]也要加6,因为说了,如果超界那投得次数也要算上. #include <iostream>#include <functional>#include <algorithm>#incl

HackerRank - &quot;Snakes and Ladders: The Quickest Way Up&quot;

A trickier Dijkstra. #include <cmath> #include <cstdio> #include <vector> #include <string> #include <iostream> #include <algorithm> #include <queue> #include <unordered_map> #include <unordered_set> u

LightOJ - 1151概率dp+高斯消元

概率dp+高斯消元 https://vjudge.net/problem/LightOJ-1151 题意:刚开始在1,要走到100,每次走的距离1-6,超过100重来,有一些点可能有传送点,可以传送到前面或后面,那么概率dp没法递推,只能高斯消元 设期望E(x),首先100这个位置的期望E(100)=0,然后可以找出方程, 对于传送点,E(x)=E(go(x)),对于非传送点,E(x)=(E(x+1)+E(x+2)+E(x+3)+E(x+4)+E(x+5)+E(x+6)+6)/cnt(cnt是可

【leetcode】909. Snakes and Ladders

题目如下: 解题思路:天坑题,不在于题目多难,而是要理解题意.题目中有两点要特别注意,一是"You choose a destination square S with number x+1, x+2, x+3, x+4, x+5, or x+6, provided this number is <= N*N." 这里最大可以移动的x+6中的6真的就是数字6啊,不是例子中的N=6的6,可以理解成是掷骰子.二是"Note that you only take a snak

Codeforces Round #597 (Div. 2) E. Hyakugoku and Ladders 概率dp

E. Hyakugoku and Ladders Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple just playing "Cat's Cradle" so now she w

Swift流程控制之循环语句和判断语句详解

Swift提供了所有c类语言的控制流结构.包括for和while循环来执行一个任务多次:if和switch语句来执行确定的条件下不同的分支的代码:break和continue关键字能将运行流程转到你代码的另一个点上. 除了C语言传统的for-condition-increment循环,Swift加入了for-in循环,能更加容易的遍历arrays, dictionaries, ranges, strings等其他序列类型. Swift的switch语句也比C语言的要强大很多. Swift中swi

进击的雨燕--------------协议

详情转自:http://wiki.jikexueyuan.com/project/swift/chapter2/07_Closures.html 协议定义了一个蓝图,规定了用来实现某一特定工作或者功能所必需的方法和属性.类,结构体或枚举类型都可以遵循协议,并提供具体实现来完成协议定义的方法和功能.任意能够满足协议要求的类型被称为遵循(conform)这个协议. 除了遵循协议的类型必须实现那些指定的规定以外,还可以对协议进行扩展,实现一些特殊的规定或者一些附加的功能,使得遵循的类型能够收益. 协议

控制流

http://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/chapter2/05_Control_Flow.html 本页包含内容: For 循环 While 循环 条件语句 控制转移语句(Control Transfer Statements) Swift提供了类似 C 语言的流程控制结构,包括可以多次执行任务的for和while循环,基于特定条件选择执行不同代码分支的if和switch语句,还有控制流程跳转到其他

Swift2.1 语法指南——协议

原档: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID267 参考:http://wiki.jikexueyuan.com/project/swift/chapter2/22_Protocols.html 1.协议 协议定