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:

 #include<bits/stdc++.h>
 #define Ms(a,x) memset(a,x,sizeof a)
 using namespace std;
 ;
 int n,got[N]; double a[N][N],E[N];
 ?x:-x;}
 void Gauss(int equ,int var) {
     ,col=,cho;
     for (; row<=equ&&col<=var; row++,col++) {
         cho=row;
         ; i<=equ; i++)
             if (abso(a[i][col])>abso(a[cho][col])) cho=col;
         if (cho!=row)
             ; i++) swap(a[cho][i],a[row][i]);
         ) {col--; continue;}
         ; i<=equ; i++) ) {
             double k=a[i][col]/a[row][col];
             ; j++) a[i][j]-=k*a[row][j];
         }
     }
     for (int i=var; i; i--) {
         ];
         ; j<=var; j++) re-=a[i][j]*E[j];
         E[i]=re/a[i][i];
     }
 }
 int main() {
     int T; scanf("%d",&T);
     ; ts<=T; ts++) {
         cin>>n,Ms(got,),Ms(a,),Ms(E,);
         ,x,y; i<=n; i++)
             scanf("%d%d",&x,&y),got[x]=y;
         ,c; i<; i++) if (!got[i]) {
             c=min(,-i),a[i][i]=c,a[i][]=;
             ; j<=&&i+j<=; j++) a[i][i+j]=-;
         } ,a[i][got[i]]=-,a[i][]=;
         a[][]=,a[][]=;
         Gauss(,);
         printf(]);
     }
     ;
 }

[lightoj P1151] Snakes and Ladders的更多相关文章

  1. LightOJ - 1151 Snakes and Ladders —— 期望、高斯消元法

    题目链接:https://vjudge.net/problem/LightOJ-1151 1151 - Snakes and Ladders    PDF (English) Statistics F ...

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

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

  3. LightOJ - 1151 Snakes and Ladders

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

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

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

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

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

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

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

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

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

  8. [Swift]LeetCode909. 蛇梯棋 | Snakes and Ladders

    On an N x N board, the numbers from 1 to N*N are written boustrophedonically starting from the botto ...

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

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

随机推荐

  1. eclipse 安装 lombok插件

    下载插件 官网下载 :https://projectlombok.org/ 有些小伙伴官网可能打不开,现提供我的下载地址:https://files.cnblogs.com/files/niceyoo ...

  2. [js]js设计模式-单例模式

    单例模式 不同模块之间需要同时开发, // 单例模式: 把描述同一个事物的属性和方法放在同一个内存空间下. // 优点: 分组,防止冲突 // p1 p2也叫做命名空间(模块开发) var p1 = ...

  3. spring-boot 速成(2) devtools之热部署及LiveReload

    JRebel热部署插件相信很多人都知道,但是这是一款商业插件,spring-boot框架也提供了类似的功能,即:devtools,关键是免费的! 使用方法如下: 一.添加 devtools依赖 dep ...

  4. [macOS] macOS下,VirtualBox安装CentOS7.4, 搭建nginx, mysql, PHP5.6&PHP7.1

    准备工作 网络设置 相关教程:http://www.jianshu.com/p/e6ba699b5992 ifcfg-enp0s3配置 TYPE=Ethernet BOOTPROTO=dhcp DEF ...

  5. 详解Python变量在内存中的存储

    这篇文章主要是对python中的数据进行认识,对于很多初学者来讲,其实数据的认识是最重要的,也是最容易出错的.本文结合数据与内存形态讲解python中的数据,内容包括: 引用与对象 可变数据类型与不可 ...

  6. vue 在浏览器控制台怎么调试 谷歌插件vue Devtools

    vue 在浏览器控制台怎么调试 谷歌插件vue Devtools 问题: vuejs里面的变量,怎么用浏览器的console查看? 例如,想在chrome里用console.log查看变量$data, ...

  7. CSS 初知

    CSS 初知 举例 人 { 身高:175cm; 体重:70kg; 肤色:黄色 } 描述 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通 ...

  8. 程序员的快速开发框架:Github上 10 大优秀的开源后台控制面板

    程序员的快速开发框架:Github上 10 大优秀的开源后台控制面板 Web 开发中几乎的平台都需要一个后台管理,但是从零开发一套后台控制面板并不容易,幸运的是有很多开源免费的后台控制面板可以给开发者 ...

  9. String.split()与StringUtils.split()

    我们平时进行简单的字符串分割的时候,尽量不要用String自身的split方法,它是匹配正则表达式的,如果遇到$这种特殊字符,需要转义一下.用StringUtils.split()方法会更方便 使用a ...

  10. [C++ Primer Plus] 第5章、循环和关系表达式(二)课后习题

    1.编写一个要求用户输入两个整数的程序,将程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和.这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44 ...