hdu1885(状态压缩)

本文详细介绍了使用BFS算法解决HDU1885及类似问题的步骤与技巧,通过实例深入探讨了如何在C++中实现这一经典数据结构的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:hdu1885

刚刚做了hdu1429那道题,这两道题一样的方法

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<queue>
#define MAXN 105
using namespace std;
char map[105][105];
bool v[105][105][20];
const int d[4][2] = {{0,-1},{1,0},{0,1},{-1,0}};
int n,m,flag;
int sx,sy;
struct node
{
    int x,y;
    int step;
    int key;
};
int deal(char c)
{
    int n,j = 1;
    if(c >= 'a' && c <= 'z') n = c - 'a';
    else n = c - 'A';
    while(n--)
        j <<= 1;
    return j;
}
void bfs()
{
    memset(v,0,sizeof(v));
    queue <node> q;
    node s,temp;
    s.x = sx;
    s.y = sy;
    s.step = 0;
    s.key = 0;
    q.push(s);
    v[s.x][s.y][s.key] = 1;
    while(!q.empty())
    {
        temp = q.front();
        q.pop();
        if(map[temp.x][temp.y] == 'X')
        {
            flag = 1;
            printf("Escape possible in %d steps.\n",temp.step);
            return;
        }
        for(int i = 0; i < 4; i ++)
        {
            s.x = temp.x + d[i][0];
            s.y = temp.y + d[i][1];
            s.key = temp.key;
            if(v[s.x][s.y][s.key] || s.x < 0 || s.x >= n || s.y < 0 || s.y >= m || map[s.x][s.y] == '#')
                continue;

            s.step = temp.step + 1;
            v[s.x][s.y][s.key] = 1;
            if(map[s.x][s.y]>='a'&&map[s.x][s.y]<='d')
            {
                int num = deal(map[s.x][s.y]);
                if((s.key&num) == 0)
                    s.key |= num;
                q.push(s);
            }
            else if(map[s.x][s.y]>='A'&&map[s.x][s.y]<='D')
            {
                int num = deal(map[s.x][s.y]);
                if(s.key&num)
                    q.push(s);
            }
            else q.push(s);
        }
    }
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m),(m+n))
    {
        for(i = 0; i < n; i ++)
        {
            scanf("%s",map[i]);
            for(j = 0; j < m; j ++)
            {
                if(map[i][j] == '*')
                    sx = i, sy = j, map[i][j] = '.';
                else if(isalpha(map[i][j]))
                {
                    switch(map[i][j])
                    {
                        case 'G': map[i][j] = 'A'; break;
                        case 'g': map[i][j] = 'a'; break;
                        case 'Y': map[i][j] = 'C'; break;
                        case 'y': map[i][j] = 'c'; break;
                        case 'R': map[i][j] = 'D'; break;
                        case 'r': map[i][j] = 'd'; break;
                        default: break;
                    }
                }
            }
        }
        flag = 0;
        bfs();
        if(!flag) printf("The poor student is trapped!\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值