标签:less end memory ade 高斯消元 1.5 splay move else
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.
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 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.
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.
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
主要题意就不解释了。。
我们设从点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 }
[lightoj P1151] Snakes and Ladders
标签:less end memory ade 高斯消元 1.5 splay move else
原文地址:http://www.cnblogs.com/whc200305/p/7688061.html