hdu 2128 BFS

这题太恶心了。。。。。不对。。。是我弱暴了。。。。。我一直以为炸了墙之后墙还有的来着。。。好吧我蛋疼了。。。。

AC代码如下:

// hdu 2128 mnlm 1.0  
#include<cstdio>  
#include<cstdlib>  
#include<cmath>  
#include<cstring> 
#include <cctype>
#include<vector>  
#include<algorithm>  
using namespace std;  
#include<queue>  
  
struct NODE  
{  
    int x;  
    int y;  
    int b;  
    int t;  
    int mb[8][8]; //保存每个位置的最小炸弹数  
};  
queue <NODE> q;  
int xy[4][2] =   
{  
    {0, 1},  
    {0, -1},  
    {1, 0},   
    {-1, 0}  
};  
int N;  
int M;  
int startx, starty;  //S的位置  
char map[10][10];  
int ans;             //最小的时间,初始值为-1  
void BFS()  
{  
    ans = -1;  
    while (!q.empty())  
    {  
        q.pop();  
    }  
    NODE m;  
    NODE tt;  
    m.x = startx;  
    m.y = starty;  
    m.b = 0;  
    m.t = 0;  
    memset(m.mb, -1, sizeof(m.mb));  
    m.mb[m.x][m.y] = 0;  
    q.push(m);  
    while (!q.empty())  
    {  
        m = q.front();  
        q.pop();  
        if (ans != -1 &&  
        m.t + 1 >= ans)  
        {  
            continue;  
        }  
        int i;   
        for (i = 0; i < 4; i++)  
        {  
            tt.x = m.x + xy[i][0];  
            tt.y = m.y + xy[i][1];  
            tt.b = -1;  
            if (tt.x >= 0 && tt.x < N &&   
            tt.y >= 0 && tt.y < M)  
            {  
                if (map[tt.x][tt.y] == 'D')  
                {  
                    if (ans == -1 ||  
                    m.t + 1 < ans)  
                    {  
                        ans = m.t + 1;  
                    }  
                    continue;  
                }  
                if (m.mb[tt.x][tt.y] > -1 ||  
                map[tt.x][tt.y] == '.') //已经走过的炸弹和WALL都没有了,相当于'.'  
                {  
                    tt.t = m.t + 1;  
                    tt.b = m.b;  
                }  
                else if (map[tt.x][tt.y] == 'X' &&  
                m.b > 0)  
                {  
                    tt.t = m.t + 2;  
                    tt.b = m.b - 1;  
                }  
                else if (isdigit(map[tt.x][tt.y]))  
                {  
                    tt.t = m.t + 1;  
                    tt.b = m.b + map[tt.x][tt.y] - '0';  
                }
                if (tt.b > m.mb[tt.x][tt.y])  
                {  
                    memcpy(tt.mb, m.mb, 64 * sizeof(int));  
                    tt.mb[tt.x][tt.y] = tt.b;  
                    q.push(tt);  
                }
            }  
        }  
    }  
    return;  
}  
int main()  
{  
    while (1)  
    {  
        scanf("%d%d", &N, &M);  
        if (!N && !M)  
        {  
            break;  
        }  
        int i;  
        int j;  
        for (i = 0; i < N; i++)  
        {  
            scanf("%s", map[i]);  
            for (j = 0; j < M; j++)  
            {  
                if (map[i][j] == 'S')  
                {  
                    startx = i;  
                    starty = j;  
                }  
            }  
        }  
        BFS();  
        printf("%d\n", ans);  
    }  
}  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值