hdu1026 Ignatius and the Princess I(bfs+路径)


http://acm.hdu.edu.cn/showproblem.php?pid=1026

题意:求最短路长度、路径,有打怪操作,同样在路径中输出。


思路:很好的练习题吧,今天(2016/10/24)第二次做,打怪地方的标记初始化放在了结构体中,半天找不出错。好在改好了,1A~。


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>

using namespace std;

typedef long long LL;

const int N = 105;
const int INF = 0x3f3f3f3f;

char G[N][N];
bool vis[N][N];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, m;

struct node
{
    int x, y, step, prex, prey;
    int flag;
    friend bool operator < (const node &a, const node &b)
    {
        return a.step > b.step;
    }
};

node path[N][N];

bool check(int x, int y)
{
    if(x>=0 && x<n && y>=0 && y<m && !vis[x][y] && G[x][y]!='X') return true;
    else return false;
}

void bfs(int x, int y)
{
    memset(vis, false, sizeof(vis));
    priority_queue<node>que;
    node s;
    s.x = x;
    s.y = y;
    s.step = 0;
    vis[x][y] = true;
    que.push(s);
    while(!que.empty())
    {
        node tmp = que.top();
        que.pop();
        if(tmp.x==n-1 && tmp.y==m-1)
        {
            path[tmp.x][tmp.y] = tmp;
            break;
        }
        for(int i = 0; i < 4; i++)
        {
            node tmp2;
            tmp2 = tmp;
            tmp2.x += dir[i][0];
            tmp2.y += dir[i][1];
            tmp2.step += 1;
            if(check(tmp2.x, tmp2.y))
            {
                vis[tmp2.x][tmp2.y] = true;
                if(isdigit(G[tmp2.x][tmp2.y]))
                {
                    tmp2.step += (G[tmp2.x][tmp2.y]-'0');
                    tmp2.flag = (G[tmp2.x][tmp2.y]-'0');
                }
                else tmp2.flag = 0;//注意这里对flag赋值,不能在初始化结构体中赋值,不然输出有错
                tmp2.prex = tmp.x;
                tmp2.prey = tmp.y;
                que.push(tmp2);
                path[tmp2.x][tmp2.y] = tmp2;
            }
        }
    }
}

void Print()
{
    stack<node>sta;
    node now = path[n-1][m-1];
    if(now.step == 0) printf("God please help our poor hero.\n");
    else
    {
        sta.push(now);
        printf("It takes %d seconds to reach the target position, let me show you the way.\n", now.step);
        while(1)
        {
            now = path[now.prex][now.prey];
            sta.push(now);
            if(now.x==0 && now.y==0) break;
        }
        int time = 1;
        sta.pop();
        while(!sta.empty())
        {
            now = sta.top();
            sta.pop();
            if(now.flag == 0)
                printf("%ds:(%d,%d)->(%d,%d)\n", time++, now.prex, now.prey, now.x, now.y);
            else
            {
                printf("%ds:(%d,%d)->(%d,%d)\n", time++, now.prex, now.prey, now.x, now.y);
                for(int i = 1; i <= now.flag; i++)
                {
                    printf("%ds:FIGHT AT (%d,%d)\n", time++, now.x, now.y);
                }
            }
        }
    }
    printf("FINISH\n");
}


int main()
{
   // freopen("in.txt", "r", stdin);
    while(~scanf("%d%d", &n, &m))
    {
        memset(path, 0, sizeof(path));
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                cin >> G[i][j];
            }
        bfs(0, 0);
        Print();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值