Hdu1254 push box (bfs + bfs)

Source: Internet
Author: User

Hdu1254 push box (bfs + bfs)
Push boxTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 6386 Accepted Submission (s): 1825



Problem Description is a classic game. today we are going to play a simple version. in a M * N room, there is a box and a porter. the porter's job is to push the box to the specified position. Note that the Porter can only push the box but not pull it, therefore, if the box is pushed to a corner (2), the box cannot be moved. If the box is pushed to a wall, the box can only move along the wall.

Given the structure of the room, the location of the box, the location of the Porter and the location of the box to be pushed, Please calculate the minimum number of bins the porter should push.



The first line of Input data is an integer T (1 <= T <= 20), representing the number of test data. then there is the T group of test data. The first row of each group of test data is two positive integers M, N (2 <= M, N <= 7), representing the size of the room, then there is a matrix of M rows and N columns, which represents the layout of the room. 0 represents an empty floor, 1 represents a wall, and 2 represents the start position of the box, 3 indicates the position of the box to be pushed, and 4 indicates the start position of the porter.

Output for each group of test data, the Output Porter must push at least the number of cells in the box to help the box push to the specified location. If the box cannot be pushed to the specified location, the Output is-1.

Sample Input
15 50 3 0 0 01 0 1 4 00 0 1 0 01 0 2 0 00 0 0 0 0

Sample Output
4

Author Ignatius. L & weigang Lee
Analysis: bfs nested bfs: a tracing for searching people and a tracing for searching boxes.
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      # Include
       
         # Include
        
          # Include
         
           # Include using namespace std; const double eps = 1e-6; const double pi = acos (-1.0); const int INF = 0x3f3f3f; const int MOD = 1000000007; # define ll long # define CL (a, B) memset (a, B, sizeof (a) int n, m; int mat [11] [11], dir [4] [2] = {}, {0,-1 }}; bool vis [11] [11] [4], vm [11] [11]; int man_x, man_y, box_x, box_y, en_x, en_y; struct A {int x, y ;}; struct node {int step; struct A box, man ;}; bool bb (int B _x, int B _y, int m_x, int m_y, int k) // whether the searcher can reach the push location {CL (vm, false); queue
          
            Qq; node ss, tt; ss. man. x = m_x; ss. man. y = m_y; vm [ss. man. x] [ss. man. y] = true; vm [B _x] [B _y] = true; // the location of the current box indicates that qq cannot be accessed. push (ss); while (! Qq. empty () {ss = qq. front (); qq. pop (); if (ss. man. x + dir [k] [0] = B _x & ss. man. y + dir [k] [1] = B _y) return true; for (int I = 0; I <4; I ++) {tt. man. x = ss. man. x + dir [I] [0]; tt. man. y = ss. man. y + dir [I] [1]; if (mat [tt. man. x] [tt. man. y]! = 0) continue; if (! Vm [tt. man. x] [tt. man. y]) {vm [tt. man. x] [tt. man. y] = true; qq. push (tt) ;}}return false;} void bfs () // search the trajectory of the box {queue
           
             Q; node now, next; now. box. x = box_x; now. box. y = box_y; now. man. x = man_x; now. man. y = man_y; now. step = 0; q. push (now); while (! Q. empty () {now = q. front (); q. pop (); if (now. box. x = en_x & now. box. y = en_y) {cout <
            
              > T; while (T --) {CL (mat,-1); // map initialization. the boundary is-1 cin> n> m; for (int I = 1; I <= n; I ++) {for (int j = 1; j <= m; j ++) {cin> mat [I] [j]; if (mat [I] [j] = 2) // The location of people, boxes, and endpoints is well stored, then, change the corresponding location in the map to walk, that is, 0 {box_x = I; box_y = j; mat [I] [j] = 0 ;} if (mat [I] [j] = 3) {en_x = I; en_y = j; mat [I] [j] = 0 ;} if (mat [I] [j] = 4) {man_x = I; man_y = j; mat [I] [j] = 0 ;}} CL (vis, false); bfs ();} return 0 ;}
            
           
          
         
        
       
     
    
   
  
 


 

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.