It is simple to ask for the problem of state transfer equation;
Dp[i][j][k] means walk I step to J position the rightmost position is k so it's easy to know k>=j;
S: probability l go left probability R go right
if k==j;
Dp[i][j][k]=dp[i-1][j][k]*s+dp[i-1][j-1][k-1]*r+dp[i-1][j-1][k]*r;
if k!=j;
Dp[i][j][k]=dp[i-1][j][k]*s+dp[i-1][j-1][k]*r+sp[i-1][j+1][k]*r;
If the N value reaches 1000 and dp[1001][2001][2001] is not open, then the dynamic array is used, because the next step is only related to the previous one, so open dp[2][2001][2001].
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <
algorithm> #include <cmath> #include <vector> #include <queue> using namespace std;
Double l,r,s;
Double dp[2][300][300];
int main () {int t,n;
scanf ("%d", &t);
for (int i1=1;i1<=t;i1++) {scanf ("%d%d%lf%lf", &i1,&n,&l,&r);
S=1-l-r;
Memset (Dp,0,sizeof (DP));
int cur=1;
int last=0;
Dp[0][100][100]=1;
for (int i=1;i<=n;i++) {cur=last^1;
for (int j=100-i;j<=100+i;j++) {for (int k=max (100,J); k<=100+i;k++) {
if (j==k) dp[cur][j][k]=dp[last][j][k]*s+dp[last][j-1][k-1]*r+dp[last][j-1][k]*r;
else dp[cur][j][k]=dp[last][j][k]*s+dp[last][j-1][k]*r+dp[last][j+1][k]*l; }} last=Cur
} double sum=0; for (int i=100-n;i<= (100+n), i++) {for (int j=100;j<= (100+n); j + +) {su
m+=dp[cur][i][j]* (j-100);
}} printf ("%d%.4lf\n", i1,sum);
} return 0;
}