hdu1026

这题花了很长时间,代码还是有点乱~~

#include<stdio.h>
#include<string.h>
#include<queue>
#include<ctype.h>
using namespace std;
#define INF 10000000
char map[105][105];
int d[105][105];
int visit[105][105];
int xx[4]={1,-1,0,0};
int yy[4]={0,0,1,-1};
struct node
{
    int x,y;
};
node pa[105][105];
void print(node t1,node t2,int time);
int n,m;
int main()
{

    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                d[i][j]=INF;
        }
        memset(visit,0,sizeof(visit));
        queue<node> q;
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='X')
                    visit[i][j]=1;
            }
        }
        d[0][0]=0;
        node t1,t2;
        t1.x=0;t1.y=0;
        q.push(t1);
        while(!q.empty())
        {
            t1=q.front();
            q.pop();
            for(i=0;i<4;i++)
            {
                t2.x=xx[i]+t1.x;
                t2.y=yy[i]+t1.y;
                if(t2.x>=0&&t2.x<n&&t2.y>=0&&t2.y<m&&!visit[t2.x][t2.y])
                {
                    if(map[t2.x][t2.y]=='.')
                    {
                        if(d[t1.x][t1.y]+1<d[t2.x][t2.y])
                        {
                            d[t2.x][t2.y]=d[t1.x][t1.y]+1;
                            pa[t2.x][t2.y].x=t1.x;
                            pa[t2.x][t2.y].y=t1.y;
                            q.push(t2);
                        }
                    }
                    else if(isdigit(map[t2.x][t2.y]))
                    {
                        if(d[t1.x][t1.y]+1+map[t2.x][t2.y]-'0'<d[t2.x][t2.y])
                        {
                            d[t2.x][t2.y]=d[t1.x][t1.y]+1+map[t2.x][t2.y]-'0';
                            pa[t2.x][t2.y].x=t1.x;
                            pa[t2.x][t2.y].y=t1.y;
                            q.push(t2);
                        }
                    }
                }
            }
        }
        if(d[n-1][m-1]==INF)
            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",d[n-1][m-1]);

            node t1,t2;

            t1.x=0;t1.y=0;
            t2.x=n-1;t2.y=m-1;
            print(t1,t2,d[n-1][m-1]);

        }
        printf("FINISH\n");
    }
    return 0;
}
void print(node t1,node t2,int time)
{
    int s=time;
    if(t1.x==t2.x&&t1.y==t2.y)
        return;
    else
    {
        if(t2.x==n-1&&t2.y==m-1&&isdigit(map[t2.x][t2.y]))
            s=s-(map[t2.x][t2.y]-'0');
        if(isdigit(map[pa[t2.x][t2.y].x][pa[t2.x][t2.y].y]))
            s=s-(map[pa[t2.x][t2.y].x][pa[t2.x][t2.y].y]-'0');
        print(t1,pa[t2.x][t2.y],s-1);
        if(isdigit(map[pa[t2.x][t2.y].x][pa[t2.x][t2.y].y]))
        {
            int i;
            for(i=0;i<map[pa[t2.x][t2.y].x][pa[t2.x][t2.y].y]-'0';i++)
                printf("%ds:FIGHT AT (%d,%d)\n",s++,pa[t2.x][t2.y].x,pa[t2.x][t2.y].y);
        }
        printf("%ds:(%d,%d)->(%d,%d)\n",s,pa[t2.x][t2.y].x,pa[t2.x][t2.y].y,t2.x,t2.y);
        if(t2.x==n-1&&t2.y==m-1&&isdigit(map[t2.x][t2.y]))
        {
            int i;
            for(i=0;i<map[t2.x][t2.y]-'0';i++)
                printf("%ds:FIGHT AT (%d,%d)\n",++s,t2.x,t2.y);
        }
    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值