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

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.

  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的所抛骰子次数的期望值。

题解:

代码如下

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const double eps = 1e-;
const int MOD = 1e9+;
const int MAXN = +; double a[MAXN][MAXN], x[MAXN];
int Gauss(int equ, int var)
{
int i, j, k, col, max_r;
for(k = ,col = ; k<=equ&&col<=var; k++,col++)
{
max_r = k;
for(i = k+; i<=equ; i++)
if(fabs(a[i][col])>fabs(a[max_r][col]))
max_r = i;
if(fabs(a[max_r][col])<eps) return ;
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+; j<=var; j++) a[k][j] /= a[k][col];
a[k][col] = ;
for(i = ; i<=equ; i++)
if(i!=k)
{
x[i] -= x[k]*a[i][k];
for(j = col+; j<var; j++) a[i][j] -= a[k][j]*a[i][col];
a[i][col] = ;
}
}
return ;
} int nxt[MAXN];
int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
memset(nxt, , sizeof(nxt));
for(int i = ; i<=n; i++)
{
int u, v;
scanf("%d%d", &u,&v);
nxt[u] = v;
} memset(a, , sizeof(a));
memset(x, , sizeof(x));
for(int i = ; i<; i++)
{
if(nxt[i])
{
a[i][i] = ;
a[i][nxt[i]] = -;
x[i] = ;
}
else
{
int cnt = ;
for(int j = ; i+j<=&&j<=; j++)
{
cnt++;
a[i][i+j] = -;
}
a[i][i] = cnt; x[i] = ;
}
}
a[][] = ; x[] = ;
Gauss(,);
printf("Case %d: %.10lf\n", ++kase, x[]);
}
}

LightOJ - 1151 Snakes and Ladders —— 期望、高斯消元法的更多相关文章

  1. LightOJ 1151 Snakes and Ladders 期望dp+高斯消元

    题目传送门 题目大意:10*10的地图,不过可以直接看成1*100的,从1出发,要到达100,每次走的步数用一个大小为6的骰子决定.地图上有很多个通道 A可以直接到B,不过A和B大小不确定   而且 ...

  2. LightOJ - 1151 Snakes and Ladders

    LightOJ - 1151 思路: 将期望dp[x]看成自变量,那么递推式就可以看成方程组,用高斯消元求方程组的解就能求解出期望值 高斯消元求解的过程也是期望逆推的过程,注意边界情况的常数项,是6/ ...

  3. LightOJ 1151 - Snakes and Ladders 高斯消元+概率DP

    首先来个期望的论文,讲的非常好,里面也提到了使用线性方程组求解,尤其适用于有向图的期望问题. 算法合集之<浅析竞赛中一类数学期望问题的解决方法> http://www.lightoj.co ...

  4. LightOJ 1151 Snakes and Ladders(概率DP + 高斯消元)

    题意:1~100的格子,有n个传送阵,一个把进入i的人瞬间传送到tp[i](可能传送到前面,也可能是后面),已知传送阵终点不会有另一个传送阵,1和100都不会有传送阵.每次走都需要掷一次骰子(1~6且 ...

  5. LightOJ - 1151 Snakes and Ladders(概率dp+高斯消元)

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

  6. [lightoj P1151] Snakes and Ladders

    1151 - Snakes and Ladders Time Limit: 2 second(s)    Memory Limit: 32 MB 'Snakes and Ladders' or 'Sh ...

  7. light oj 1151 - Snakes and Ladders 高斯消元+概率DP

    思路: 在没有梯子与蛇的时候很容易想到如下公式: dp[i]=1+(∑dp[i+j])/6 但是现在有梯子和蛇也是一样的,初始化p[i]=i; 当有梯子或蛇时转移为p[a]=b; 这样方程变为: dp ...

  8. Snakes and Ladders LightOJ - 1151( 概率dp+高斯消元)

    Snakes and Ladders LightOJ - 1151 题意: 有100个格子,从1开始走,每次抛骰子走1~6,若抛出的点数导致走出了100以外,则重新抛一次.有n个格子会单向传送到其他格 ...

  9. LightOJ 1030 Discovering Gold(期望)

    Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...

随机推荐

  1. 粗略。。Java项目设计模式之笔记----studying

    设计模式 设计模式:解决这个问题的一种行之有效的思想. 设计模式:用于解决特定环境下.反复出现的特定问题的解决方式. 设计模式学习概述 ★ 为什么要学习设计模式 1.设计模式都是一些相对优秀的解决方式 ...

  2. android(cm11)状态栏源代码分析(一)

    (一):写在前面 近期因为工作须要,须要了解CM11中的有关于StatusBar相关的内容.总的来说,刚開始阅读其源代码的时候,是有点困难,只是通过构建相关代码的脑图和流程图,几天下来.我已经对其源代 ...

  3. Android性能优化Google课程翻译一:Render----OverDraw实战

    Context 近期实战了下OverDraw,加深了下理解.在上篇文章里Android性能优化Google课程翻译一:Render----OverDraw 写过详细方法. OverDraw解决方法离不 ...

  4. AutoIT V3如何修改字体

    1 如图所示,文字很小,阅读和编写多很吃力.   2 按住Ctrl,鼠标滚轮上下滚动可以改变字体大小.   3 如何修改界面的字体,比如改为微软雅黑的字体,现在还没有一个好的解决方案,大家先将就着用吧 ...

  5. 怎样使用 iOS 7 的 AVSpeechSynthesizer 制作有声书(3)

    plist 中的每一页 utteranceSting 我们都创建了一个RWTPage.displayText.因此,每页的文本会一次性地显示出来. 由于 You've constructedeach ...

  6. hdu-5015-233 Matrix-矩阵

    非常显然矩阵的第一列为: 0 a[1] a[2] a[3] a[4] 我们转化一下,转化为 23 a[1] a[2] a[3] a[4] 3 那么由第一列转移到第二列则为 23*10+3 a[1]+2 ...

  7. 图论 Krusal算法C++实现

    1.实验用例 如下图所示的赋权图表示某七个城市及预先算出它们之间的一些直接通信成路造价(单位:万元),试给出一个设计方案,使得各城市之间既能够通信又使总造价最小并计算其最小值. 2实验原理和方法 为了 ...

  8. 线性表的链式实现(C++)

    相关内容: 线性表的顺序实现 链式实现(C语言) (推荐在GitHub上查看,下面代码只是最初实现,后续如果有修改,由于太麻烦,不会再更改下文,仅仅更新Github上的代码文件) 结点以及链表类的定义 ...

  9. ArcMap中使用ArcPy实现Geometry与WKT的相互转换

    在Web GIS迅猛发展的今天,使用浏览器来进行交互以其方便性.快捷性被广大用户所推崇,那么在传输格式方面,都已比較简单的JSON或者WKT来解决网络带宽带来的数据压力. 在ArcGIS10.2版本号 ...

  10. 【Servlet与JSP】请求转发与重定向

    假设一个登录系统,要求用户输入用户名和密码: 用户在上面表单当中输入了信息之后,点击登录按钮(type="submit")将表单作为请求参数进行提交. 这一提交就有两种形式:get ...