Lightoj 1151 Snakes and Ladders Gaussian elimination element

Source: Internet
Author: User

Give you a map 10*10, from 1 to 100, ask the number of times you want to roll the dice, there will be a portal from A to B.

Gaussian elimination of the basic problem, learned a board, x storage more, a storage factor;

Either dp[i]= (dp[i+1]+...+dp[i+6]+6)/6;

either Dp[i]=dp[go[i]];

It is worth noting that even if there is no dp[i+6] also need to add 6, because said, if the super-bounds that the number of shots to be counted.

#include <iostream>
#include <functional>
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace Std;

#define EPS 1e-9
const int maxn=220;
Double A[MAXN][MAXN],X[MAXN];
int Equ,var;
int go[110];
int back[110];
int Gauss ()
{
int i,j,k,col,max_r;
for (k = 0,col = 0; k < equ && Col < var; k++,col++)
{
Max_r = k;
for (i = k+1; i < equ; i++)
if (Fabs (A[i][col]) > Fabs (A[max_r][col]))
Max_r = i;
if (Fabs (A[max_r][col]) < EPS) return 0;
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+1; J < var; j + +) A[k][j]/=a[k][col];
A[k][col] = 1;
for (int i = 0; i < equ; i++)
if (i! = k)
{
X[i]-= x[k]*a[i][k];
for (j = col+1; J < var; j + +) A[i][j]-= A[k][j]*a[i][col];
A[i][col] = 0;
}
}
return 1;
}

int main ()
{
int I,j,ncas=1;
int t;
Double a[110][110];
scanf ("%d", &t);
while (t--)
{
memset (A,0.0,sizeof (a));
memset (x,0.0,sizeof (x));
for (int i=0;i<100;i++) x[i]=1;
memset (go,-1,sizeof (go));
equ=var=100;

int all;
scanf ("%d", &all);
while (all--)
{
int t1,t2;
scanf ("%d%d", &t1,&t2);
t1--;
t2--;
Go[t1]=t2;
}
for (int i=0; i<100; i++)
{
if (go[i]==-1)
{
if (i<=93)
{
a[i][i]=6.0;
a[i][i+1]=-1.0;
a[i][i+2]=-1.0;
a[i][i+3]=-1.0;
a[i][i+4]=-1.0;
a[i][i+5]=-1.0;
a[i][i+6]=-1.0;
x[i]=6;
}
else if (i==99)
{
a[i][i]=1.0;
x[i]=0.0;
}
Else
{
a[i][i]= (99-i) *1.0;
int epx=1;
while (i+epx<=99)
{
a[i][i+epx]=-1.0;
epx++;
}
x[i]=6*1.0;
}
}
Else
{
a[i][i]=1.0;
a[i][go[i]]=-1.0;
x[i]=0;
}
}
Gauss ();
printf ("Case%d:%.10f\n", ncas++,x[0]);
}
return 0;
}

Lightoj 1151 Snakes and Ladders Gaussian elimination element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.