题目链接:http://codeforces.com/contest/24/problem/D
题意:一个n*m的格子,初始时在(X,Y)。每一步,保持不动、左移、右移、下移的概率相等。求移动到最后一行的期望。
思路:设p[i][j]表示从(i,j)到结束的期望,则:
j=1,p[i][j]=(p[i][j]+p[i][j+1]+p[i+1][j])/3+1
2<=j<=m-1,p[i][j]=(p[i][j]+p[i][j-1]+p[i][j+1]+p[i+1][j])/4+1
j=m,p[i][j]=(p[i][j]+p[i][j-1]+p[i+1][j])/3+1
将p[i][j]移到左边,令c[1]=c[m]=p[i+1][j]/2+1.5,c[j]=p[i+1][j]/3+4/3(2<=j<=m-1),则:
j=1,p[i][j]=p[i][j+1]/2+c[1]
2<=j<=m-1,p[i][j]=p[i][j-1]/3+p[i][j+1]/3+c[j]
j=m,p[i][j]=p[i][j-1]/2+c[m]
得到方程组。下面解方程组。
import java.util.*;
import java.text.*;
import java.math.*;
public class Main{
static double EPS=1e-10;
static double PI=Math.acos(-1.0);
static double p[][]=new double[1005][1005];
static double A[]=new double[1005];
static double B[]=new double[1005];
static double C[]=new double[1005];
static int n,m,x,y;
public static void PR(String s){
System.out.println(s);
}
public static void PR(int x)
{
System.out.println(x);
}
public static void PR(double s)
{
java.text.DecimalFormat d=new java.text.DecimalFormat("#.000000000");
System.out.println(d.format(s));
}
public static double cal()
{
if(m==1) return 2.0*(n-x);
int i,j;
double temp;
for(i=1;i<=m;i++) p[n][i]=0;
for(i=n-1;i>=x;i--)
{
C[1]=p[i+1][1]/2+1.5;
C[m]=p[i+1][m]/2+1.5;
for(j=2;j<=m-1;j++) C[j]=p[i+1][j]/3+4.0/3;
A[1]=1; B[1]=-0.5;
for(j=2;j<=m-1;j++)
{
temp=1.0/3/A[j-1];
A[j]=temp*B[j-1]+1;
B[j]=-1.0/3;
C[j]+=temp*C[j-1];
}
temp=0.5/A[j-1];
A[j]=temp*B[j-1]+1;
C[j]+=temp*C[j-1];
p[i][m]=C[m]/A[m];
for(j=m-1;j>=1;j--) p[i][j]=(C[j]-p[i][j+1]*B[j])/A[j];
}
return p[x][y];
}
public static void main(String[] args){
Scanner S=new Scanner(System.in);
while(S.hasNext())
{
n=S.nextInt();
m=S.nextInt();
x=S.nextInt();
y=S.nextInt();
PR(cal());
}
}
}