HDU 1429 状压bfs 胜利大逃亡

4 篇文章 0 订阅

题目链接:HDU 1429





题解:这题 是状压bfs入门题,但是实在我知道是状压bfs之后了。。。这种最短路一般是bfs,第一眼看到有状态问题,就想着bfs,无果 然后联想到状压dp, 一点点找状态,最后 好不容易做出答案 提交 MLE...懵逼 开数组标记状态的时候 用的1 << 10 + 5 ..哎。。


状压bfs,这题找到钥匙 ,要判断 遇到锁要看有没有钥匙

step[i][j][k]表示在i, j点状态为k的最优解

AC code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<queue>
using namespace std;

#define debug 0
#define inf 0x3f3f3f3f

const int maxn = 20 + 5;
char str[maxn][maxn];
int steps[maxn][maxn][(1 << 10) + 5], n, m, tl, stx, sty;
int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};//数组标记方向

struct R
{
    int x, y;
    int status;
    int step;
} s, t;
bool judge(int x, int y)
{
    if(x < 1 || x > n || y < 1 || y > m)
        return false;
    return true;
}
int tran(char ch)
{
    int ans = 1;
    char a = isupper(ch)? 'A' : 'a';
    int n = ch - a;
    while(n--)
        ans *= 2;
    return ans;
}
int bfs()
{
    s.step = 0;
    s.status = 0;
    s.x = stx;
    s.y = sty;
    steps[s.x][s.y][s.status] = 0;	//初始化 起点状态步数优先解为0
    queue<R> q;
    q.push(s);
    while(!q.empty())
    {
        t = q.front();
        q.pop();
        if(str[t.x][t.y] == '^')
        {
            return t.step;//规定时间内找到出口
        }
        for(int i = 0; i < 4; i++)
        {
            s.x = t.x + dir[i][0], s.y = t.y + dir[i][1];
            s.status = t.status;
            s.step = t.step + 1;//下一步的状态
            //printf("status:%d\n",s.status);
            if(s.step >= tl)		//剪枝
                continue;
            if(judge(s.x, s.y))//边界
            {
                if(s.step >= steps[s.x][s.y][s.status]) continue;//找的是最优解
                steps[s.x][s.y][s.status] = s.step;
                char ch = str[s.x][s.y];
                if(ch == '*')//不可走
                    continue;
                else if(islower(ch))
                {
                    int temp = tran(ch);
                    if((s.status & temp) == 0)//位运算优先级 刚开始找bug在这里浪费好多时间 没加()
                    {
                        s.status += temp;//第一次找到该钥匙更新状态
                        //printf("yes\n");
                    }
                    q.push(s);
                }
                else if(isupper(ch))
                {
                    int temp = tran(ch);
                    if((s.status & temp) == 0)//找到带锁门,如果有钥匙可行
                        continue;
                    q.push(s);
                }
                else
                {
                    q.push(s);
                }
            }

        }
    }
    return -1;
}
int main()
{
#if debug
    freopen("in.txt", "r", stdin);
#endif // debug
    char a;

    while(~scanf("%d%d%d", &n, &m, &tl))
    {
        memset(steps, inf, sizeof(steps));
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
            {
                while((a = getchar()) == ' ' || a == '\n');
                str[i][j] = a;
                if(a == '@')
                {
                    stx = i;
                    sty = j;
                }
            }
        //printf("%d %d\n", stx, sty);
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值