State compression (1)--hdu5094 (state compression +BFS) (ability problem) __hdu5094

Source: Internet
Author: User
Maze Time limit:2000/1000 MS (java/others) Memory limit:100000/ 100000 K (Java/others)



Problem DescriptionThis story happened on the background of Star Trek.

Spock, the deputy captain of Starship Enterprise, fell into Klingon ' s trick and is held as prisoner on their mother plane T Qo ' NoS.

The captain of Enterprise, James T. Kirk, had to fly to Qo ' NoS to rescue his deputy. Fortunately, he stole a map of the maze where Spock is put in exactly.

The maze is a rectangle, which has n rows vertically and M columns horizontally, in another words, which it is divided into N*m locations. A ordered pair (Row No., Column No.) represents a location in the maze. Kirk moves from the location to next costs 1 second. Able to move to next location if and only if:

Next location is adjacent to current Kirk's location on/down or left/right (4 directions)
Open door is passable, but locked door isn't.
Kirk cannot pass a wall

There are p types of doors which are locked by default. A key is only capable of opening the same type of doors. Kirk has to get the key before opening corresponding doors, which wastes little time.

Initial Location of Kirk is (1, 1) while Spock is on location of (n, m). Your task is to help Kirk find Spock as soon as possible.
InputThe input contains many test cases.

Each test case consists of several lines. Three integers are in the "the", which represent N, M and p respectively (1<= N, M <=50, 0<= p <=10).
Only one was listed in the second line, means the sum of gates and walls, (0<= K <=500).

There are 5 integers in the following k lines, represents x I1, y i1, x i2, y i2, G I; When G I >=1, represents there is a gate of type GI between location (x i1, y I1) and (x I2, y I2); When g i = 0, represents there is a wall between location (x i1, y I1) and (x I2, y i2), (| x i1-x i2 | + | y i1-y i2 |=1, 0<= g i <=p)

Following line is a integer S, represent the total number of keys in maze. (0<= S <=50).

There are three integers in the following S lines, represents x I1, y I1 and Q I respectively. That is means the key type of Q I locates on location (x i1, y I1), (1<= Q i<=p).
OutputOutput the possible minimal second that Kirk could reach Spock.

If There is no possible plan, output-1.

Sample Input
The 4 4 9 9 1 2 1 3 2 1 2 2 2 0 2 1-----------------------+--- 1
Sample Output
14 The main idea: given a chessboard, go from (1,1) to (n,m), the sides of any two squares as: access, doors, walls. Access can go directly, the door must be early to the corresponding key, the wall can never pass. The key is in some given point of the lattice (the same lattice may have many keys), only to go to these squares to get the key, ask how to take the way to get the minimum number of moving steps, and also note that two of the lattice may have multiple doors. Solution: Vis[x][y][s] Indicates whether the arrival (X,y) point has arrived when the state is S, and s represents the status of the key's getting status. And then proceed to BFS.
#include <stdio.h> #include <string.h> #include <queue> using namespace std;   int b[]={1,2,4,8,16,32,64,128,256,512,1024,2048};  Each decimal number corresponds to a binary representation of the corresponding key or gate int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};  
Up and down four directions struct node {int x,y,step,key;  
 }; 
The wall array represents a two-point gate or wall Map array that represents the point owned by the key hash array as a token int wall[51][51][51][51],map[101][101],hash[51][51][2050];  
int n,m;  
    int BFS ()//wide Search {queue<node>q;  
    Node Cur,next;  
  
    int i,w;  
    Cur.x=1;  
    Cur.y=1;  
    Cur.step=0;  
    cur.key=0;  
    CUR.KEY|=MAP[1][1];  
    Q.push (cur);  
    memset (hash,0,sizeof (hash));  
  
    Hash[1][1][cur.key]=1;  
        while (!q.empty ()) {Cur=q.front ();  
  
        Q.pop ();  
            for (i=0;i<4;i++) {next.x=cur.x+dir[i][0];  
            NEXT.Y=CUR.Y+DIR[I][1];  
            if (next.x<1 | | next.y<1 | | next.x>n | | next.y>m) continue;  
 W=WALL[CUR.X][CUR.Y][NEXT.X][NEXT.Y];           if ((w&1) ==1) continue;  If there is a wall between the two lattices, you cannot from one lattice to another lattice if (w>0 && (cur.key&w)!=w) continue;  
            Judge whether the owning key can open all the doors between the Next.key=cur.key;  
            NEXT.KEY|=MAP[NEXT.X][NEXT.Y];  
            if (hash[next.x][next.y][next.key]==1) continue;  
            Hash[next.x][next.y][next.key]=1;  
            next.step=cur.step+1;  
            if (next.x==n && next.y==m) return next.step;  
        Q.push (next);  }} return-1;  
    Cannot reach} int main () {int k,i,x1,x2,y1,y2,g,s,x,y,p;  
        while (scanf ("%d%d%d", &n,&m,&p)!=eof) {scanf ("%d", &k);  
        memset (wall,0,sizeof (wall));  
            for (i=1;i<=k;i++) {scanf ("%d%d%d%d%d", &x1,&y1,&x2,&y2,&g);  
            WALL[X1][Y1][X2][Y2]|=B[G];  
        WALL[X2][Y2][X1][Y1]|=B[G];  
        } memset (map,0,sizeof (map));  
   scanf ("%d", &s);     for (i=1;i<=s;i++) {scanf ("%d%d%d", &x,&y,&g);  
        MAP[X][Y]|=B[G]; 
        } if (N==1 && m==1) {printf ("0\n"); continue;}  
		
    printf ("%d\n", 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.