题目链接: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. 二叉查找树BST----java实现

                                                                    二叉查找树BST----java实现 1.二叉查找树简单介绍 二叉查找树 ...

  2. JAVA_Error initializing endpoint怎么办

    1 运行CMD,输入命令netstat –ano,找到这个TCP,后缀为8080的PID(我的是2660),然后打开任务管理器,在进程选项卡中打开PID查看   2 在右侧的进程窗口找到PID是左侧的 ...

  3. PHP执行linux系统命令

    本文是第一篇,讲述如何在PHP中执行系统命令从而实现一些特殊的目的,比如监控服务器负载,重启MySQL.更新SVN.重启Apache等.第二篇<PHP监控linux服务器负载>:http: ...

  4. Android开发人员不得不收集的代码(转)

    App相关→AppUtils.java 安装App installApp 卸载指定包名的App uninstallApp 获取当前App信息 getAppInfo 获取所有已安装App信息 getAl ...

  5. Android微信分享功能实例+demo

    Android微信分享功能实例 1 微信开放平台注册 2 获得appId,添加到程序中,并运行程序 3 使用应用签名apk生成签名,添加到微信开放平台应用签名,完成注册 4 测试分享功能. 有问题请留 ...

  6. Laravel 5.4---后端数据分页和前端显示分页结果

    后端数据(Eloquent 模型)分页 事先建立好Eloquent 模型和Controller 还有 前台的View.可以参考我之前的文章:Laravel建站03--建立前台文章列表和文章详情 在co ...

  7. 强大易用的日期和时间库 Joda Time

    Joda-Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time.可以利用它把JDK Date和Calendar类完全替换掉,而且仍然能够提供很好的集成,并且它是线程安全 ...

  8. Spring中的scope配置和@scope注解

    Scope,也称作用域,在 Spring IoC 容器是指其创建的 Bean 对象相对于其他 Bean 对象的请求可见范围.在 Spring IoC 容器中具有以下几种作用域:基本作用域(single ...

  9. Chrome自带恐龙小游戏的源码研究(一)

    目录 Chrome自带恐龙小游戏的源码研究(一)——绘制地面 Chrome自带恐龙小游戏的源码研究(二)——绘制云朵 Chrome自带恐龙小游戏的源码研究(三)——昼夜交替 Chrome自带恐龙小游戏 ...

  10. wifi认证Portal开发系列(二):FreeRadius的安装和测试、关联Mysql

    注:本次安装是基于FreeRadius 3版本进行安装配置的,在配置Mysql的过程中,与2版本有些不同.操作系统是CentOS 7 一.准备工作 工具的安装 #安装rz.sz命令用于文件上传 yum ...