[lightoj P1151] Snakes and Ladders

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:

 1 #include<bits/stdc++.h>
 2 #define Ms(a,x) memset(a,x,sizeof a)
 3 using namespace std;
 4 const int N=105;
 5 int n,got[N]; double a[N][N],E[N];
 6 double abso(double x) {return x>0?x:-x;}
 7 void Gauss(int equ,int var) {
 8     int row=1,col=1,cho;
 9     for (; row<=equ&&col<=var; row++,col++) {
10         cho=row;
11         for (int i=row+1; i<=equ; i++)
12             if (abso(a[i][col])>abso(a[cho][col])) cho=col;
13         if (cho!=row)
14             for (int i=col; i<=var+1; i++) swap(a[cho][i],a[row][i]);
15         if (abso(a[row][cho])<1e-6) {col--; continue;}
16         for (int i=row+1; i<=equ; i++) if (abso(a[i][col])>1e-10) {
17             double k=a[i][col]/a[row][col];
18             for (int j=col; j<=var+1; j++) a[i][j]-=k*a[row][j];
19         }
20     }
21     for (int i=var; i; i--) {
22         double re=a[i][var+1];
23         for (int j=i+1; j<=var; j++) re-=a[i][j]*E[j];
24         E[i]=re/a[i][i];
25     }
26 }
27 int main() {
28     int T; scanf("%d",&T);
29     for (int ts=1; ts<=T; ts++) {
30         cin>>n,Ms(got,0),Ms(a,0),Ms(E,0);
31         for (int i=1,x,y; i<=n; i++)
32             scanf("%d%d",&x,&y),got[x]=y;
33         for (int i=1,c; i<100; i++) if (!got[i]) {
34             c=min(6,100-i),a[i][i]=c,a[i][101]=6;
35             for (int j=1; j<=6&&i+j<=100; j++) a[i][i+j]=-1;
36         } else a[i][i]=1,a[i][got[i]]=-1,a[i][101]=0;
37         a[100][100]=1,a[100][101]=0;
38         Gauss(100,100);
39         printf("Case %d: %.10lf\n",ts,E[1]);
40     }
41     return 0;
42 }
View Code

 

转载于:https://www.cnblogs.com/whc200305/p/7688061.html

代码转载自:https://pan.quark.cn/s/7f503284aed9 Hibernate的核心组件总数达到五个,具体包括:Session、SessionFactory、Transaction、Query以及Configuration。 这五个核心组件在各类开发项目中都具有普遍的应用性。 借助这些组件,不仅可以高效地进行持久化对象的读取与存储,还能够实现事务管理功能。 接下来将通过图形化的方式,逐一阐述这五个核心组件的具体细节。 依据所提供的文件内容,可以总结出以下几个关键知识点:### 1. SSH框架详细架构图尽管标题提及“SSH框架详细架构图”,但在描述部分并未直接呈现关于SSH的详细内容,而是转向介绍了Hibernate的核心接口。 然而,在此我们可以简要概述SSH框架(涵盖Spring、Struts、Hibernate)的核心理念及其在Java开发中的具体作用。 #### Spring框架- **定义**:Spring框架是一个开源架构,其设计目标在于简化企业级应用的开发流程。 - **特点**: - **分层结构**:该框架允许开发者根据实际需求选择性地采纳部分组件,而非强制使用全部功能。 - **可复用性**:Spring框架支持创建可在不同开发环境中重复利用的业务逻辑和数据访问组件。 - **核心构成**: - **核心容器**:该部分包含了Spring框架的基础功能,其核心在于`BeanFactory`,该组件通过工厂模式运作,并借助控制反转(IoC)理念,将配置和依赖管理与具体的应用代码进行有效分离。 - **Spring上下文**:提供一个配置文件,其中整合了诸如JNDI、EJB、邮件服务、国际化支持等企业级服务。 - **Spring AO...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值