题目链接: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. HDFS冗余数据块的自动删除

    HDFS冗余数据块的自动删除 在日常维护hadoop集群的过程中发现这样一种情况: 某个节点由于网络故障或者DataNode进程死亡,被NameNode判定为死亡,HDFS马上自动开始数据块的容错拷贝 ...

  2. m3u8文件信息总结

    近期在做视频下载.本地播放功能的时候.发现的问题,先笔记记录一下 开发思路 (1) 在线解析m3u8文件内容,把里面的ts相应连接的资源下载本地的Document文件下. (2) 把下载下来的资源使 ...

  3. 4pda.ru注冊验证的解码算法

    代码源于看雪林版在我群里介绍注冊一个俄文安卓论坛.发出来了链接大家在測试注冊. http://4pda.ru/forum/index.php? 註册方式請参看: _https://forum.tuts ...

  4. java GC(Garbage Collector) | System.gc()

    http://win.sy.blog.163.com/blog/static/94197186201151093543556/     Java垃圾回收调优

  5. 鼠标点击input框后里面的内容就消失

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. chm文件打不开的解决办法

    我今天在网上找了找C++函数库,下载下来一个 .chm 文件,打开之后发现只显示了目录,内容却显示不出来. 显示是这样:右边区域显示不出来. 在网上查了一下发现CHM文件是网上比较多的电子书籍显示格式 ...

  7. 应用处理器AP概述

    移动终端芯片其它部分见"一站式了解智能终端处理器". 功能机时代,扩展手机特性是在基带芯片上进行.手段包含:升级基带芯片获得更强的计算能力.电路进行又一次设计以添加功能如照相机和S ...

  8. 加入 centos 右键 terminal

    centos6.2以上默认右键都没有terminal,现加入方法 例如以下 1>  yum -y install nautilus-open-terminal 2> shutdown -r ...

  9. erlang的token值加解密

    对于加解密,需客户端和服务器制定好对应的规则(如:加密算法(aes,des等).加密模式(cbc,cfb)),去加密,再按逆序列解密.这里的key是根据数字.大小写字母.符合组合的,每次请求获取一个动 ...

  10. 认识 service worker

    离线缓存可以提升用户体验,可以节省网络资源,但是,浏览器对资源缓存和自定义网络请求的控制一直不够完善,service worker 的出现就是为了解决这些问题 它可以解决目前离线应用的问题,同时也可以 ...