9 degree oj question 1546: Maze problem (probability dp guess elimination)

Source: Internet
Author: User

Question link: Click the open link

Description:

Given an n * m maze, such
S ..
..#
E. E
S represents the start position, # represents the wall that cannot be walked, and E represents the table exit.
The hero starts from the starting position, and selects the next walking position at random for each probability until it reaches a certain exit.
Now he wants to know the expected number of steps from the start point to a certain exit in this probability event.

Input:

The input contains multiple groups of test cases. Each group of test cases starts with two integers n, m (1 <= n, m <= 15), representing the size of the maze.
The following n lines describe the maze information by m characters in each line. The specific rules are described in the topic.
Data must have at least one E and one S, but multiple E may exist.

Output:

Output A floating point number, which indicates the expected number of steps out of the maze and retains two decimal places. If the hero cannot walk out of the maze, output-1.

Sample input:
1 2SE2 2S..E1 3S#E
Sample output:
1.004.00-1
Tip:

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 */
                         
                        
                       
                      
                     
                    
                   
                 
                
               
              
             


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.