10.15

1159:Maz总时间限制: 2000ms 内存限制: 65536kB
描述
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.
输入
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.
输出
For each test case, in one line output “YES” if Acm can find the treasure, or “NO” otherwise.
样例输入
4 4
S.X.
a.X.
..XG
….
3 4
S.Xa
.aXB
b.AG
0 0
样例输出
YES
NO
题目大意就是给你一个迷宫,里面有钥匙,有门,最多有五种,然后你必须收集到这个门的所有钥匙才能开门,比如迷宫中有4把‘a’钥匙,那么要打开‘A’号门需要拿到4把’a’钥匙。
花了一下午就搞了一个这玩意,bfs搜索奉上。dfs明天补上。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int dx[]={0,1,0,-1,0};
const int dy[]={0,0,1,0,-1};
char map[50][50];
bool used[50][50];
int n,m,sx,sy,ex,ey;
struct dqs
{
    int x,y;
};
struct dqm
{
    int x,y,key,vis;
}door[500];
queue <dqs> q;
void km(int zz)   // 开门如果访问过,直接把这个点压入队列,如果没有让他变成点
{
    if(door[zz].vis==1)
    {
        dqs pp;
        pp.x=door[zz].x;
        pp.y=door[zz].y;
        q.push(pp);
    }
    else 
        map[door[zz].x][door[zz].y]='.';
}
void bfs(int x,int y)
{
    while (!q.empty()) q.pop();   //多组数据清空队列
    dqs f;
    f.x=x,f.y=y;
    q.push(f);
    used[x][y]=1;
    while (!q.empty())
    {
        f=q.front();
        q.pop();
        //cout<<f.x<<" "<<f.y<<endl;    
        if(f.x==ex&&f.y==ey)
        {
            cout<<"YES"<<endl; 
            return ;
        }
        for(int i=1;i<=4;i++)
        {
            int nx=f.x+dx[i];
            int ny=f.y+dy[i];
            if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&map[nx][ny]!='X'&&used[nx][ny]==0)
            {
                used[nx][ny]=1;
                dqs nxt;
                nxt.x=nx;
                nxt.y=ny;
                if(map[nx][ny]>='a'&&map[nx][ny]<='e') //如果是钥匙
                {
                    int la=map[nx][ny]-97;
                    door[la].key--;
                    if(door[la].key==0)
                        km(la); 
                    q.push(nxt);
                }
                else if(map[nx][ny]>='A'&&map[nx][ny]<='E') //如果是门
                {
                    door[map[nx][ny]-65].vis=1;
                }
                else    //如果是点或G
                    q.push(nxt);
            }           
        }
    }   
    cout<<"NO"<<endl;
    return ;
}

void init()
{   
    memset(map,0,sizeof(map));
    memset(used,0,sizeof(used));
    memset(door,0,sizeof(door));
}
int main()
{
    while (scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
            break;
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",map[i]+1);
            for(int j=1;j<=m;j++)
            {
                if(map[i][j]=='S')
                    sx=i,sy=j;
                if(map[i][j]=='G')
                    ex=i,ey=j;
                if(map[i][j]>='a'&&map[i][j]<='e')
                    door[map[i][j]-97].key++; //0 a=97
                if(map[i][j]>='A'&&map[i][j]<='E') 
                {
                    door[map[i][j]-65].x=i;
                    door[map[i][j]-65].y=j;     //0 A=65 
                }
            }       
        }
        bfs(sx,sy);
    }   

    return 0;
}

老是打错字母,以后慢点打认真点,调不出来,认真捋捋思路,认真检查代码。

上午打了个差分,跟模拟。洛谷上的题都a掉了。
今天日记被一个题带过吧。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值