首页 > 其他 > 详细

*Hdu 1026-Ignatius and the Princess I

时间:2014-06-18 00:23:27      阅读:440      评论:0      收藏:0      [点我收藏+]
题目思想比較简单,可是题目要求输出路径比較麻烦,学习了Ice_Crazy博主的方法,感觉不错。
#include <iostream>
#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
const int maxn = 100 + 5;
struct node
{
    int x,y;
    int step;
    friend bool operator<(node n1,node n2)
    {
        return n1.step>n2.step;
    }
};
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int map[maxn][maxn];
int flag[maxn][maxn];
int blood[maxn][maxn];
int n,m;
int ptime;

int BFS()
{
    priority_queue<node> Q;
    node s;
    s.x = 0;
    s.y = 0;
    s.step = 0;
    map[0][0] = -1;
    Q.push(s);

    while(!Q.empty())
    {
        if(Q.top().x==n-1 && Q.top().y==m-1)
            return Q.top().step;
        node next;
        for(int i=0;i<4;i++)
        {
            next.x = Q.top().x + dir[i][0];
            next.y = Q.top().y + dir[i][1];

            if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && map[next.x][next.y]!=-1)
            {
                next.step = Q.top().step+1+map[next.x][next.y];

                flag[next.x][next.y] = i+1;
                map[next.x][next.y] = -1;
                Q.push(next);
            }
        }
        Q.pop();
    }
    return -1;
}
void Path(int x,int y)
{
    int nx_x;
    int nx_y;
    if(flag[x][y] == 0)
        return ;
    nx_x = x - dir[flag[x][y]-1][0];
    nx_y = y - dir[flag[x][y]-1][1];
    Path(nx_x,nx_y);
    printf("%ds:(%d,%d)->(%d,%d)\n",ptime++,nx_x,nx_y,x,y);
    while(blood[x][y]--)
        printf("%ds:FIGHT AT (%d,%d)\n",ptime++,x,y);
}
int main()
{
    char str[maxn];
    int ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(map,0,sizeof(map));
        memset(flag,0,sizeof(flag));
        memset(blood,0,sizeof(blood));
        for(int i=0;i<n;i++)
        {
            scanf("%s",str);
            for(int j=0;str[j];j++)
            {
                if(str[j]=='.')
                    map[i][j]=0;
                else if(str[j]=='X')
                    map[i][j]=-1;
                else
                    map[i][j]=blood[i][j]=str[j]-'0';
            }
        }

        ans=BFS();
        //printf("AA\n");
        if(ans==-1)
            printf("God please help our poor hero.\n");
        else
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
            ptime=1;
            Path(n-1,m-1);
        }
        printf("FINISH\n");
    }

    return 0;
}

*Hdu 1026-Ignatius and the Princess I,布布扣,bubuko.com

*Hdu 1026-Ignatius and the Princess I

原文:http://www.cnblogs.com/hrhguanli/p/3793435.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!