You may go back when you go.
Question:
There is a starting point S, and multiple egress E, # indicates that it cannot be taken. The probability of each random choice is the next walking location, and the expectation from S to the exit is obtained.
Ideas:
The point that bfs preprocessing can reach. If it cannot reach the end point,-1 is output; otherwise, dp is output.
Dp [I]-expectation of the number of steps required to reach the end point after arriving at I.
For each vertex x0 that can be reached, assume that the adjacent points that can be reached include x1, x2, and x3.
Dp [x0] = 1 + dp [x1]/3 + dp [x2]/3 + dp [x3];
Ps: Note that there may be multiple endpoints. The endpoints are expected to be 0.
Code:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Pragma comment (linker, "/STACK: 102400000,102400000") # define maxn 405 # define MAXN 100005 # define OO (1LL <35) -1 # define mod 1000000009 # define INF 0x3f3f3f3f # define pi acos (-1.0) # define eps 1e-6typedef long ll; using namespace std; int n, m, sx, sy, flag, cnt; double a [maxn] [maxn], x [maxn]; // the matrix on the left of the equation and the value on the right of the equation, after the solution is completed, x stores the result subscript int equ, var starting from 0; // Number of equations and number of unknowns char mp [25] [25]; int vis [25] [25]; int dx [] = {-,}; int dy [] = {,-}; struct node {int x, y;} cur, now; int Gauss () {int I, j, k, col, max_r; for (k = 0, col = 0; k
Fabs (a [max_r] [col]) max_r = I; if (fabs (a [max_r] [col])
N | y <1 | y> m) return false; return true;} void bfs () {int I, j, t, tx, ty; flag = 0; memset (vis,-1, sizeof (vis); cnt =-1; vis [sx] [sy] = ++ cnt; queue
Q; cur. x = sx, cur. y = sy; q. push (cur); while (! Q. empty () {now = q. front (); if (mp [now. x] [now. y] = 'E') flag = 1; q. pop (); for (I = 0; I <4; I ++) {tx = now. x + dx [I]; ty = now. y + dy [I]; if (isok (tx, ty) & mp [tx] [ty]! = '#' & Vis [tx] [ty] =-1) {cur. x = tx; cur. y = ty; vis [tx] [ty] = ++ cnt; q. push (cur) ;}}} void solve () {int I, j, k, t, tx, ty, ha, cxx; equ = var = cnt + 1; memset (a, 0, sizeof (a); // remember to initialize memset (x, 0, sizeof (x); for (I = 1; I <= n; I ++) {for (j = 1; j <= m; j ++) {if (vis [I] [j] =-1) continue; ha = vis [I] [j]; if (mp [I] [j] = 'E ') // expected end point is 0 {a [ha] [ha] = 1; x [ha] = 0; continue;} cxx = 0; for (k = 0; k <4; k ++) {tx = I + dx [k]; ty = j + dy [k]; if (Isok (tx, ty) & vis [tx] [ty]! =-1) {cxx ++; a [ha] [vis [tx] [ty] =-1 ;}} a [ha] [ha] = cxx; x [ha] = cxx ;}}gauss (); printf ("%. 2f \ n ", x [vis [sx] [sy]);} int main () {int I, j, t; while (~ Scanf ("% d", & n, & m) {for (I = 1; I <= n; I ++) {scanf ("% s ", mp [I] + 1); for (j = 1; j <= m; j ++) {if (mp [I] [j] ='s ') sx = I, sy = j ;}} bfs (); if (! Flag) printf ("-1 \ n"); else {solve () ;}} return 0;}/* 1 2SE2 2S .. E1 3 S # E */