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);
        }
    }

}


你可能感兴趣的:(hdu1026)