HDU 1885 bfs+ State compression

Source: Internet
Author: User
Tags integer numbers

Key Task Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1239 Accepted Submission (s): 495


Problem DescriptionThe Czech Technical University is rather old-you already know that it celebrates years of its exi Stence in 2007. Some of the university buildings is old as well. And the navigation in old buildings can sometimes is a little bit tricky, because of strange long corridors that fork and Join at absolutely unexpected places.

The result is this some first-graders has often di?culties finding the right-to-their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the the A-labyrinth. Your task is to write a verification software the solves this game.

The Labyrinth is a 2-dimensional grid of squares and each square was either free or filled with a wall. Some of the free squares may contain doors or keys. There is four di?erent types of keys and doors:blue, yellow, red, and green. Each key can open only doors of the same color.

You can move between adjacent free squares vertically or horizontally, diagonal movement are not allowed. You are not on go across walls and you cannot leave the labyrinth area. If a square contains a door, you could go there only if you had stepped on a square with an appropriate key before.
Inputthe input consists of several maps. Each map begins with a line containing the integer numbers R and C (1≤r, c≤100) specifying the map size. Then there is R lines each containing C characters. Each character is one of the following:



Note that it's allowed to has
  • More than one exit,
  • No exit at all,
  • more doors and/or keys of the same color, and
  • Keys without corresponding doors and vice versa.

    Assume that the marker of your position ("*") would appear exactly once in every map.

    There is a blank line after each map. The input is terminated by and zeros in place of the map size.

  • Outputfor each map, print one line containing the sentence ' Escape possible in S steps. ', where S is the smallest possible Number of step to reach any of the exits. If No exit can be reached, the output of the string "the poor student is trapped!" instead.

    One step is defined as a movement between and adjacent cells. Grabbing a key or unlocking a door does not count as a step.
    Sample Input
    1 10* ... X1 #X3 20#################### #XY. gbr.*. rb.g.gg.y#################### #0 0

    Sample Output
    Escape possible in 9 steps. The poor student is trapped! Escape possible in steps.

    Source2008 "Shun Yu Cup" Zhejiang Collegiate Programming Contest-warm up (2)


    /*************************************************************************> File name:3.cpp> Author:yuan > mail:> Created time:2014 November 26 Wednesday 13:09 34 seconds ************************************************************** /#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib>#    Include<algorithm> #include <queue>using namespace Std;char mat[105][105];int ans;struct node{int x, y; int num,key;};    BOOL vis[105][105][30];queue<node> q;int sx,sy;int n,m,top,base;int next[4][2]={1,0,0,1,-1,0,0,-1};void BFS () {    int X1,x2,y1,y2;node p,qq;      while (!q.empty ()) {P=q.front ();        X1=P.X;Y1=P.Y;            if (mat[x1][y1]== ' X ') {ans=p.num;        Return            } for (int i=0;i<4;i++) {int key;            X2=X1+NEXT[I][0];Y2=Y1+NEXT[I][1]; if (x2<0| | x2>=n| | y2<0| | y2>=m| |            mat[x2][y2]== ' # ') continue; if (mat[x2][y2]== '. ' | | mat[x2][y2]== ' * ' | | mat[x2][y2]==' X ') {key=p.key;                    if (vis[x2][y2][key]==0) {qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key;                    Q.push (QQ);                Vis[x2][y2][key]=1; }} else if (mat[x2][y2]== ' B ' | | mat[x2][y2]== ' Y ' | | mat[x2][y2]== ' R ' | |                mat[x2][y2]== ' G ') {key=p.key;                    if (vis[x2][y2][key]==0) {int D;                    if (mat[x2][y2]== ' B ') d=0;                           else if (mat[x2][y2]== ' Y ') d=1;                                  else if (mat[x2][y2]== ' R ') d=2;                    else if (mat[x2][y2]== ' G ') d=3;                    int lock=1<<d;                         if (Lock&key) {qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key;                         Q.push (QQ);                    Vis[x2][y2][key]=1; }}} and else if (mat[x2][y2]== ' B ' | | mat[x2][y2]== ' Y ' | | mat[x2][y2]== ' r ' | |                    mat[x2][y2]== ' G ') {int D;                    if (mat[x2][y2]== ' B ') d=0;                           else if (mat[x2][y2]== ' y ') d=1;                                  else if (mat[x2][y2]== ' R ') d=2;                else if (mat[x2][y2]== ' G ') d=3;                key=1<<d;                    if (vis[x2][y2][p.key]==0) {qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key|key;                    Q.push (QQ);                Vis[x2][y2][p.key]=1;    }}} q.pop ();        }}int Main () {while (1) {scanf ("%d%d", &n,&m);        if (n==0&&m==0) break;        memset (Mat,0,sizeof (MAT));        memset (vis,0,sizeof (VIS));        while (!q.empty ()) Q.pop ();        for (int i=0;i<n;i++) scanf ("%s", Mat[i]);        for (int i=0;i<n;i++) for (int j=0;j<m;j++) {if (mat[i][j]== ' * ') {sx=i,sy=j;}        } node p; P.x=sx;p.y=sy;p.num=0;p.key=0;        Q.push (P);        Vis[sx][sy][0]=1;        ans=100000000;        BFS ();        if (ans<100000000) printf ("Escape possible in%d steps.\n", ans);    else printf ("The poor student is trapped!\n"); } return 0;}


    HDU 1885 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.