HDU 5094--maze "BFS && State Compression"

Source: Internet
Author: User

MazeTime limit:2000/1000 MS (java/others) Memory limit:100000/100000 K (java/others)
Total submission (s): 903 Accepted Submission (s): 316


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 The maze where Spock is put in exactly.

The maze is a rectangle, which have n rows vertically and M columns horizontally, in another words, that's it's divided into N*m locations. An ordered pair (Row No., Column No.) represents a location in the maze. Kirk moves from the current location to next costs 1 second. And he is able to move to next location if and only if:

Next location was adjacent to the current Kirk's location on up or down or left or right (4 directions)
Open door is passable, but locked door are not.
Kirk cannot pass a wall

There is P types of doors which is locked by default. A key is only capable of opening the same type of doors. Kirk have 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 is in the first line, which represent N, M and p respectively (1<= N, M <=50, 0<= p <=10).
Only one of the integer k are listed in the second line, means the sum number of gates and walls, (0<= K <=500).

There is 5 integers in the following K lines, represents Xi1, Yi1, Xi2, Yi2, GI; When Gi >=1, represents there are a gate of type GI between location (Xi1, YI1) and (Xi2, YI2); When GI = 0, represents there are a wall between location (Xi1, YI1) and (Xi2, Yi2), (| xi1-xi2 | + | Yi1-yi2 |=1, 0&L t;= gi <=p)

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

There is three integers in the following S lines, represents xi1, Yi1 and Qi respectively. That means the key type of Qi locates on location (XI1, Yi1), (1<= qi<=p).
Outputoutput the possible minimal second that Kirk could reach Spock.

If There is no possible plan, output-1.

Sample Input
4 4 991 2 1 3 21 2 2 2 02 1 2 2 02 1 3 1 02 3 3 3 02 4 3 4 13 2 3 3 03 3 4 3 04 3 4 4 022 1 24 2 1


The map array is initialized to-1, the test sample is not over, debugging for 2 hours to find, it is more and more than.

Also note: A place may be more than one different type of key, pit AH

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 55using namespace Std;int vis[maxn][maxn][1 << 11];int map[maxn][maxn][maxn][maxn];int keynum[maxn][maxn][11];// What kinds of keys are recorded in this location int dir[4][2] = {0, 1, 0,-1, 1, 0,-1, 0};int N, M, p, S, k;//p represents several keys.    struct node{int x, y, step, key;    friend bool Operator < (Node A, Node B) {return a.step > b.step;    }};int Check (Node A, Node B) {if (b.x <= 0 | | b.x > N | | b.y <= 0 | | b.y > m) return 0;    if (map[a.x][a.y][b.x][b.y] = = 0) return 0; return 1;}    int BFS () {priority_queue<node>q;    Node now, next;    now.x = 1;    NOW.Y = 1;    Now.step = 0;    Now.key = 0; for (int j = 1; J <=; ++j) {//Start with key if (keynum[now.x][now.y][j]) Now.key = Now.key |    (1 << j);    } q.push (now);    Vis[now.x][now.y][now.key] = 1;        while (!q.empty ()) {now = Q.top ();        Q.pop (); if (now.x = = N &Amp;& now.y = = m) {return now.step;            } for (int i = 0; i < 4; ++i) {next.x = now.x + dir[i][0];            Next.y = Now.y + dir[i][1];            Next.step = Now.step + 1;            printf ("--%d\n", next.step);            Next.key = Now.key;                    if (check (now, next)) {//now and Next have no wall if (Map[now.x][now.y][next.x][next.y] > 0) {//now and next have doors in the middle                        if (Next.key & (1 << map[now.x][now.y][next.x][next.y)) {//There is a key to this door that can reach next through this door                                for (int j = 1; J <=; ++j) {//Judgment point next is there a key if (Keynum[next.x][next.y][j]) Next.key = Next.key |                        (1 << j);                             } if (!vis[next.x][next.y][next.key]) {Vis[next.x][next.y][next.key] = 1;                        Q.push (next);        }}} or else {            for (int j = 1; J <=; ++j) {//Judgment point next is not a key if (Keynum[next.x][next.y][j]) { Next.key = Next.key |                        (1 << j); }} if (!vis[next.x][next.y][next.key]) {Vis[next.x][next.                            Y][next.key] = 1;                    Q.push (next); }}}}} return-1;}        int main () {while (scanf ("%d%d%d", &n, &m, &p)! = EOF) {scanf ("%d", &k);        memset (Map,-1, sizeof (map));        memset (Vis, 0, sizeof (VIS));        memset (keynum, 0, sizeof (keynum));            while (k--) {int x1, y1, x2, y2, t;            scanf ("%d%d%d%d%d", &x1, &y1, &x2, &y2, &t);            Map[x1][y1][x2][y2] = t;        MAP[X2][Y2][X1][Y1] = t;        } scanf ("%d", &s);            while (s--) {int x, y, t; scanf ("%d%d%d", &x, &y,&AMP;T);        Keynum[x][y][t] = 1;    } printf ("%d\n", BFS ()); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5094--maze "BFS && State Compression"

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.