OpenJudge noi 1159Maze(poj 2157)

Description
Acm, a treasure-explorer, is exploring again. This time he is in a special maze, in which there are some doors (at most 5 doors, represented by ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ respectively). In order to find the treasure, Acm may need to open doors. However, to open a door he needs to find all the door’s keys (at least one) in the maze first. For example, if there are 3 keys of Door A, to open the door he should find all the 3 keys first (that’s three ‘a’s which denote the keys of ‘A’ in the maze). Now make a program to tell Acm whether he can find the treasure or not. Notice that Acm can only go up, down, left and right in the maze

Input

The input consists of multiple test cases. The first line of each test case contains two integers M and N (1 < N, M < 20), which denote the size of the maze. The next M lines give the maze layout, with each line containing N characters. A character is one of the following: ‘X’ (a block of wall, which the explorer cannot enter), ‘.’ (an empty block), ‘S’ (the start point of Acm), ‘G’ (the position of treasure), ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ (the doors), ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ (the keys
of the doors). The input is terminated with two 0’s. This test case should not be processed.

Output

For each test case, in one line output “YES” if Acm can find the
treasure, or “NO” otherwise.

Sample Input

4 4
S.X.
a.X.
..XG
….
3 4
S.Xa
.aXB
b.AG
0 0
Sample Output

YES
NO

bfs:
这个题,可以单独开一个结构体,记录门的位置。在输入过程中,我们先把门处理一下,放入结构体中,然后将图遍历一遍,再处理每个门的钥匙个数。在bfs的过程中,如果我们找到了门,但是没有找够钥匙,我们就把这个门的vis记录为1,如果可以找够钥匙,我们就再把这个门的坐标放到队列中,让它再继续拓展;若我们在找够钥匙之后,发现门还没有没访问过,我们就直接把门变为一条路,直到我们找到目标位置。

最重要的一点:初始化的时候队列别忘了清空!!!mdzz,这是第二次犯这种问题了,谨记!!!

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=550;
int dx[]={0,1,0,-1,0};
int dy[]={0,0,1,0,-1};
struct dqs
{
    int x,y;
};
struct dqm
{
    int x,y,key;
    bool vis;
}door[maxn];
char map[maxn][maxn];
bool use[maxn][maxn];
int sx,sy;  
int n,m;
queue<dqs>q;
void open(int z)
{
    if(door[z].vis==1)
    {
        dqs nxt;
        nxt.x=door[z].x;
        nxt.y=door[z].y;
        q.push(nxt);        
    }
    else
        map[door[z].x][door[z].y]='.';
}
bool check(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=m&&map[x][y]!='X'&&use[x][y]==0)
        return true;
    return false;
}
void bfs()
{
    while(!q.empty()) q.pop();
    dqs begin;
    begin.x=sx,begin.y=sy;
    q.push(begin);
    use[sx][sy]=1;
    while(!q.empty())
    {
        dqs head=q.front();
        q.pop();
        dqs now;
        for(int i=1;i<=4;i++)
        {
            now.x=head.x+dx[i];
            now.y=head.y+dy[i];
            if(check(now.x,now.y))
            {
                if(map[now.x][now.y]=='G')
                {
                    printf("YES\n");
                    return;
                }
                else
                {
                    use[now.x][now.y]=1;
                    if(map[now.x][now.y]>='a'&&map[now.x][now.y]<='e')
                    {
                        q.push(now);
                        int ss=map[now.x][now.y]-'a'+1;
                        door[ss].key--;
                        if(door[ss].key==0)
                            open(ss);
                    }
                    if(map[now.x][now.y]>='A'&&map[now.x][now.y]<='E')
                    {
                        int ss=map[now.x][now.y]-'0'-16;
                        door[ss].vis=1;
                    }
                    if(map[now.x][now.y]=='.')
                        q.push(now);
                }           
            }
        }
    }
    printf("NO\n");
    return;
}
int main()
{
    string a;
    while(1)
    {
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)
        break;
        memset(map,0,sizeof(map));
        memset(use,0,sizeof(use));
        memset(door,0,sizeof(door));
        for(int i=1;i<=n;i++)
        {
            cin>>a;
            for(int j=0;j<m;j++)
            {
                map[i][j+1]=a[j];
                if(map[i][j+1]>='A'&&map[i][j+1]<='E')
                {
                    int ss=map[i][j+1]-'0'-16;
                    door[ss].x=i;
                    door[ss].y=j+1;
                    door[ss].vis=0;
                    door[ss].key=0;
                }       
            }
        }
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(map[i][j]=='S')
                sx=i,sy=j;
            if(map[i][j]>='a'&&map[i][j]<='e')
                door[map[i][j]-'0'-48].key++;   
        }
        bfs();      
    }
    return 0;
} 
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值