hdu 1885 key task (搜索)

来源:互联网 发布:被一个人爱的感觉知乎 编辑:程序博客网 时间:2024/06/05 18:11

Key Task

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2000    Accepted Submission(s): 876


Problem Description
The Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places.

The result is that some first-graders have often di?culties finding the right way 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 way out of a labyrinth. Your task is to write a verification software that solves this game.

The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are 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 is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.
 

Input
The input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following:



Note that it is allowed to have
  • 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.

    You may assume that the marker of your position (“*”) will appear exactly once in every map.

    There is one blank line after each map. The input is terminated by two zeros in place of the map size.
  •  

    Output
    For 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, output the string “The poor student is trapped!” instead.

    One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.
     

    Sample Input
    1 10*........X1 3*#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 45 steps.

     

    //思路是一个bool记录某个点在某种状态下是否到达过 另一个数组记录到某点的最小值#include<iostream>#include<algorithm>#include<cstdlib>#include<cctype>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<set>#include<queue>#include<cmath>#define pi acos(-1.0)#define inf 1<<29#define INF 0x3f3f3f3f#define zero 1e-8using namespace std;const int li[]={-1,0,1,0};const int lj[]={0,-1,0,1};int val[50];bool type[17][107][107];//记录走的情况int step[107][107];//记录最小步数int r,c,ans;char map[107][107];struct lo{    int x,y;    int step;    int color;    int kinds;    lo(){}    lo(const lo& a): x(a.x),y(a.y),step(a.step),color(a.color){}    lo(int x,int y,int step,int color=0):x(x),y(y),step(step),color(color){}    bool operator < (const lo& b)const {        return step>b.step;    }    void show(){        cout<<"x:"<<x<<" y:"<<y<<" step:"<<step<<" color:"<<color<<endl;    }}que[900000];int head,tail;void todo(){    memset(type,0,sizeof(type));    memset(step,INF,sizeof(step));    head=tail=0;    ans=inf;    for (int i=0;i<r;++i)        for (int j=0;j<c;++j)            if (map[i][j]=='*'){                que[tail].x=i;                que[tail].y=j;                que[tail].step=0;                que[tail++].color=0;                step[i][j]=0;            }}inline bool iskey(int x,int y){    if (map[x][y]=='b'||map[x][y]=='y'        ||map[x][y]=='r'||map[x][y]=='g')            return true;    return false;}inline bool isdoor(int x,int y){     if (map[x][y]=='B'||map[x][y]=='Y'        ||map[x][y]=='R'||map[x][y]=='G')            return true;    return false;}inline bool judge(int x,int y,int color,int i,int& tip){    tip=0;    if (x+li[i]<0||x+li[i]>=r||            y+lj[i]<0||y+lj[i]>=c||                type[color][x+li[i]][y+lj[i]]                    ||map[x+li[i]][y+lj[i]]=='#') return false;    if (iskey(x+li[i],y+lj[i])) {        tip=val[map[x+li[i]][y+lj[i]]-'a'];        return true;    }    else if (isdoor(x+li[i],y+lj[i])){        if (color&(1<<(val[map[x+li[i]][y+lj[i]]-'A']-1))){             return true;        }        return false;    }    return true;}void bfs(){    while (head<tail){        if (!type[que[head].color][que[head].x][que[head].y])                break;        head++;    }    if (head==tail||ans!=inf) return;    lo tem(que[head]);    head++;    type[tem.color][tem.x][tem.y]=true;    step[tem.x][tem.y]=step[tem.x][tem.y]<tem.step?step[tem.x][tem.y]:tem.step;    if (map[tem.x][tem.y]=='X') { ans=step[tem.x][tem.y];return;}    for (int i=0;i<4;++i){        int tip;        if (!judge(tem.x,tem.y,tem.color,i,tip)) continue;        int m=tem.color;      //  cout<<tip<<endl;        if (tip)  m=m|(1<<(tip-1));        que[tail].x=tem.x+li[i];        que[tail].y=tem.y+lj[i];        que[tail].step=tem.step+1;        que[tail++].color=m;    }    bfs();}int main(){    val['b'-'a']=1;    val['y'-'a']=2;    val['r'-'a']=3;    val['g'-'a']=4;    while(~scanf("%d %d",&r,&c)&&(r||c)){        for (int i=0;i<r;++i)            scanf("%s",&map[i]);        todo();        bfs();        if (ans!=inf) printf("Escape possible in %d steps.\n",ans);        else printf("The poor student is trapped!\n");  }  return 0;}


    0 0
    原创粉丝点击