1280: 诡异的迷宫【BFS】

来源:互联网 发布:网络问政的特点 编辑:程序博客网 时间:2024/05/16 10:02

1280: 诡异的迷宫

时间限制: 2 秒  内存限制: 128 MB
提交: 84  解决: 12
提交 状态 

题目描述

Simple最近刷题(打游戏)刷多了,一觉醒来发现自己到了一个迷宫里,怎么也出不去了。这时传来了一句话,告诉Simple必须按顺序收集完所有的宝石,才能出迷宫。所谓的顺序,就是按照每块宝石上英文字母的顺序。迷宫里面还有一些传送门,可以传送到任意一个另外的传送门的位置。(你走到一个不是空地上的地方的时候,就一定会触发相应事件,不可拒绝,从一个传送门传送到另一个传送门不用再次传送)。每走一步花费一个单位时间,传送门到另外一个传送门不需要时间。Simple初始就在字母为A的宝石的位置上(开局一宝石,其他全靠找)。

当Simple收集完所有宝石的时候就被传送出迷宫。

Simple还要赶回去刷题(打游戏),你们能告诉他最少需要多长时间才能回去吗?如果不可能出去就输出Impossible。

输入

多组实例,每组输入一个n,表示迷宫的大小为n*n  (n <= 10)
下面n行每行n个字符 
'.'表示空地,
'#'表示墙,不可以通过
'$'表示一个传送门
大写字母表示宝石

输出

每个实例输出一个数字,表示最少需要的时间,如果不能逃出迷宫,就输出Impossible。

样例输入

5A....####...B...####C.DE.2AC.B 2A##B3A$.....$B

样例输出

153Impossible2
标程:
真是没读懂题,,,不明白。。。

/*这题就是一个简单的bfs,只是要注意的地方有很多。题目要求收集宝石只能按照英文字母的顺序,所以顺序不对的宝石当前是当作墙来看待的,是不能走的。收集过的宝石的地方,是可以重复走的,不然有的时候就不能收集完所有宝石,所以每收集一次宝石,就相当于重新跑一次bfs至于传送门,你踩到一个传送门,必须要传送到另一个传送门,此时到达另外一个传送门就不能触发效果了*/#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct door{    int x,y;}e[110];int n,cnt,ce;char a[15][15];int b[15][15];int dir[4][2] = {0,1,0,-1,1,0,-1,0};struct node{    int x,y,s,cnt;};void bfs(int x,int y){    memset(b,0,sizeof(b));    a[x][y] = '.';    b[x][y] = 1;    queue<node> q;    node t,next;    t.x = x;    t.y = y;    t.s = 0;    t.cnt = 1;    q.push(t);    while(!q.empty())    {        t = q.front();        q.pop();        if(t.cnt == cnt)        {            printf("%d\n",t.s);            return;        }        for(int i=0;i<4;i++)        {            int tx = t.x + dir[i][0];            int ty = t.y + dir[i][1];            if(tx < 0 || ty < 0 || tx >= n || ty >= n)                continue;            if(a[tx][ty] == '#' ||  b[tx][ty])                continue;            if(a[tx][ty] >= 'A' && a[tx][ty] <= 'Z' && a[tx][ty] != 'A' + t.cnt)                continue;            if(a[tx][ty] >= 'A' && a[tx][ty] <= 'Z')            {                next.x = tx;                next.y = ty;                next.s = t.s + 1;                next.cnt = t.cnt + 1;                a[tx][ty] = '.';                memset(b,0,sizeof(b));                while(!q.empty())                    q.pop();                b[tx][ty] = 1;                q.push(next);                break;            }            if(a[tx][ty] == '$')            {                for(int j=0;j<ce;j++)                {                    if(e[j].x == tx && e[j].y == ty)                        continue;                    if(b[e[j].x][e[j].y])                        continue;                    next.x = e[j].x;                    next.y = e[j].y;                    next.s = t.s + 1;                    next.cnt = t.cnt;                    b[e[j].x][e[j].y] = 1;                    q.push(next);                    continue;                }                continue;            }            next.x = tx;            next.y = ty;            next.s = t.s + 1;            next.cnt = t.cnt;            b[tx][ty] = 1;            q.push(next);        }    }    printf("Impossible\n");}int main(){    int T,i,j,x,y;    while(scanf("%d",&n)==1)    {        cnt = 0;        ce = 0;        for(i=0;i<n;i++)        {            scanf("%s",a[i]);            for(j=0;j<n;j++)            {                if(a[i][j] == 'A')                    x = i, y = j;                if(a[i][j] >= 'A' && a[i][j] <= 'Z')                    cnt++;                if(a[i][j] == '$')                {                    e[ce].x = i;                    e[ce++].y = j;                }            }        }        bfs(x,y);    }    return 0;}