hdu1254 Push box (nested wide search)

Source: Internet
Author: User

Push Box
Time limit:2000/1000ms (java/other) Memory limit:65536/32768k (Java/other)
Total submission (s): Accepted submission (s): 4
Problem description Push box is a very classic game. Let's play a simple version today. In a m*n room there is a box and a porter, the Porter's job is to push the box to the designated position, note that the porter can only push the box and not pull the box, So if the box is pushed to a corner (2) then the box can no longer be moved, and if the box is pushed onto a wall, the box can only move along the wall.

Now given the structure of the room, the position of the box, the position of the porter and the position to be pushed away, please calculate how many boxes the porter should push the box at least.

The first line of input data is an integer T (1<=t<=20), which represents the number of test data. Then the T group test data, the first line of each set of test data is two positive integer m,n (2<=m,n<=7), representing the size of the room, Then there is a M-row N-column matrix representing the layout of the room, where 0 represents an empty floor, 1 represents a wall, 2 represents the starting position of the box, 3 represents the position to be pushed, and 4 represents the starting position of the porter.

Outputs for each set of test data, the output porter needs to push the box at least to get the box to the specified position, and output-1 if it cannot be pushed to the specified position.

Sample Input15 50 3 0 0 01 0 1 4 00 0 1 0 01 0 2 0 00 0 0 0 0

Sample Output4

AUTHORIGNATIUS.L & Weigang Lee

Idea: To determine whether a person can be reached from 4 to the box position 2, and not only to judge once, because the push box after a step, the position of people will change, so every time before moving the box to judge

So here are two search, when each move in one direction of the box, it is necessary to determine from the moment the person's position can reach the box in the opposite direction of the position (people can only push the box,

Can't pull anything) notice people in the move when the box for people is also an obstacle, here need to open a visit four-dimensional array records traversed the position,

#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std;  int m,n;int map[8][8];int dir[4][2]={1,0,-1,0,0,1,0,-1};int visit[8][8][8][8];//mark traversed dot int stx1,sty1;  Beginning of man int stx2,sty2;    Beginning of the box int mun;struct nodes{int x;    int y;    int BX;    int by; int ct;};    Nodes Node1,node2;void BFs () {mun=100000;    Queue<nodes> Q;    while (!q.empty ()) Q.pop ();    visit[node1.x][node1.y][node1.bx][node1.by]=0;    Q.push (Node1);        while (!q.empty ()) {Node1=q.front ();        Q.pop ();            for (int i=0; i<4; i++) {node2.x=node1.x+dir[i][0];            NODE2.Y=NODE1.Y+DIR[I][1];            NODE2.BX=NODE1.BX;            node2.by=node1.by;            Node2.ct=node1.ct; if (node2.x<0| | node2.x>=m| | node2.y<0| |            NODE2.Y&GT;=N)//cross-border continue;            if (map[node2.x][node2.y]==1)//wall continue; if (visit[node2.x][node2.y][node2.bx][node2.by]&LT;=NODE1.CT) continue;                if (node2.x==node1.bx&&node2.y==node1.by) {node2.bx=node1.bx+dir[i][0];                NODE2.BY=NODE1.BY+DIR[I][1];                node2.ct=node1.ct+1; if (node2.bx<0| | node2.bx>=m| | node2.by<0| |                NODE2.BY&GT;=N)//cross-border continue;                if (map[node2.bx][node2.by]==1)//wall continue;                if (map[node2.bx][node2.by]==3) {if (Node2.ct<mun) Mun=node2.ct;            } Visit[node2.x][node2.y][node2.bx][node2.by]=node2.ct;            } else Visit[node2.x][node2.y][node2.bx][node2.by]=node1.ct;        Q.push (Node2);    }} if (mun==100000) printf (" -1\n");    else printf ("%d\n", Mun); return;}    int main () {int t,mns;    scanf ("%d", &t); while (t--) {to (int i=0; i<8; i++) {for (intj=0; j<8;                    J + +) {for (int k=0, k<8; k++) {for (int l=0; l<8; l++)                    {visit[i][j][k][l]=100000;        }}}} scanf ("%d%d", &m,&n); for (int i=0; i<m; i++) {for (int j=0; j<n; J + +) {scanf ("%d", &map[i]                [j]);                    if (map[i][j]==4)//person's starting point {node1.x=i;                Node1.y=j;                    } else if (map[i][j]==2)//The beginning of the box {node1.bx=i;                    Node1.by=j;                node1.ct=0;        }}} BFS ();        /* if (mns<100000) printf ("%d\n", MNS);        else printf (" -1\n");        memset (map,0,sizeof (map)); */    }}

  

hdu1254 Push box (nested wide search)

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.