hdu1026Ignatius and the Princess I(BFS + 优先队列)


模板题。

代码:

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

int n,m;
char s[105][105];
int v[105][105];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
struct path{
    int x,y;
};
struct node{
    int x,y;
    int step;
    queue<path> pa;
};
node ans;

void input()

{
    for(int i = 0;i < n;++i)
            scanf("%s",s[i]);
}
void output(int f)

{
    if(f == -1)
        printf("God please help our poor hero.\n");
    else
    {
        path fir,las;
        int cnt = 1;
        fir = ans.pa.front();
        ans.pa.pop();
        las = ans.pa.front();
        ans.pa.pop();
        printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans.step);
        printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,fir.x,fir.y,las.x,las.y);
        while(!ans.pa.empty())
        {
            if(s[las.x][las.y] != '.' && s[las.x][las.y] != 'X')
            {
                int t = s[las.x][las.y] - '0';
                while(t--)
                {
                    printf("%ds:FIGHT AT (%d,%d)\n",cnt++,las.x,las.y);
                }
            }
            fir = las;
            las = ans.pa.front();
            ans.pa.pop();
            printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,fir.x,fir.y,las.x,las.y);
        }
        if(s[las.x][las.y] != '.')
        {
             int t = s[las.x][las.y] - '0';
                while(t--)
                {
                    printf("%ds:FIGHT AT (%d,%d)\n",cnt++,las.x,las.y);
                }
        }
    }
    printf("FINISH\n");
}

struct cmp{
     bool operator()(node x,node y)
     {
         return x.step > y.step;
     }
};
int bfs()

{
    memset(v,0,sizeof(v));
    priority_queue<node,vector<node>,cmp> q;
    node f,t;
    path p;
    p.x = 0;
    p.y = 0;
    f.x = 0;
    f.y = 0;
    f.step = 0;
    f.pa.push(p);
    q.push(f);
    v[0][0] = 1;
    while(!q.empty())
    {
        node h = q.top();
        q.pop();
        if(h.x == n - 1 && h.y == m - 1)
        {
            ans = h;
            return 1;
        }

        for(int i = 0;i < 4;++i)
        {
            t.x = h.x + dx[i];
            t.y = h.y + dy[i];
            if(t.x >= 0 && t.x < n && t.y >= 0 && t.y < m && !v[t.x][t.y] && s[t.x][t.y] != 'X')
            {
                //printf("%d %d\n",t.x,t.y);
                if(s[t.x][t.y] == '.')
                {
                    t.step = h.step + 1;
                    p.x = t.x;
                    p.y = t.y;
                    t.pa = h.pa;
                    t.pa.push(p);
                    q.push(t);
                    v[t.x][t.y] = 1;
                }
                else
                {
                    int time = s[t.x][t.y] - '0';
                    t.step = h.step + time + 1;
                    p.x = t.x;
                    p.y = t.y;
                    t.pa = h.pa;
                    t.pa.push(p);
                    q.push(t);
                    v[t.x][t.y] = 1;
                }
            }
        }
    }
    return -1;
}
int main()

{
    while(~scanf("%d%d",&n,&m))
    {
        input();
        int flag = bfs();
        output(flag);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值