HDU 1026 Ignatius and the Princess I

杭电 1026 拯救公主

本来刚刚写了一次的,结果最后不知怎么没保存好,唉........直接来代码吧.

#include<stdio.h>
#include<string.h>
#define M 105
#define max 10000000
char map[M][M];
int mark[M][M];
int father[M][M][2];
int n, m;
struct node{
    int x,y,step;
} Q[M*M*M],s,p,ye[M*M];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; 
void print()
{
    int i,j,k,t=0,x,y,num,x1,y1;
    printf("It takes %d seconds to reach the target position, let me show you the way.\n", mark[n - 1][m - 1]);
    x1=n-1;
	y1=m-1;
    while(x1!=0||y1!=0)
	{
        s.x=x1;
		s.y=y1;
		s.step=mark[s.x][s.y];
        ye[++t]=s;                      
        x=father[x1][y1][0];
        y=father[x1][y1][1];
        x1=x;
		y1=y;
    }
    k=1;
    printf("1s:(0,0)->(%d,%d)\n",ye[t].x,ye[t].y);
    x=ye[t].x;
	y=ye[t].y;
	num=ye[t--].step;
    k++;
    while(t>0)
	{
        while(k<=num)
        {
			printf("%ds:FIGHT AT (%d,%d)\n",k,x,y);
			k++;
		}
        if(x!=n-1||y!=m-1) 
		{
            printf("%ds:(%d,%d)->(%d,%d)\n",k++,x,y,ye[t].x,ye[t].y);
            x=ye[t].x;
			y=ye[t].y;
			num=ye[t--].step;
        }
    }
    while(k<=num)
    {
		printf("%ds:FIGHT AT (%d,%d)\n",k,x,y);
		k++;
	}
}
void bfs()
{
    int i,j,l,h;
    s.x=0;
	s.y=0;
	s.step=0;
    l=0;
	h=0;             
    Q[l++]=s;
    for(i=0;i<=n;i++)
        for(j=0;j<=m;j++)
			mark[i][j]=max; 
    if(map[0][0]=='.')
		mark[0][0]=0;
	else
		mark[0][0]=(map[0][0]-'0');
	s.step+=mark[0][0]; 
    while(h<l)
	{
        p=Q[h++];
        for(i=0;i<4;i++)
		{
			s = p;
			s.x+=dx[i];
			s.y+=dy[i];    
            if (s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&map[s.x][s.y]!='X')           
			{
                s.step++;             
                if(map[s.x][s.y]!='.')
					s.step+=(map[s.x][s.y]-'0'); //加上杀死怪物的时间
                if(mark[s.x][s.y]>s.step)
				{
                    mark[s.x][s.y]=s.step;
                    father[s.x][s.y][0]=p.x;//前一点x坐标 
                    father[s.x][s.y][1]=p.y;//前一点y坐标 
                    Q[l++]=s;                
                }
            }
        }
    }
    if(mark[n-1][m-1]==max)
        printf("God please help our poor hero.\n");
    else print();
}

int main()
{
	int i,j;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=0;i<n;i++)
		{
			getchar();
			for(j=0;j<m;j++)
			{
				scanf("%c",&map[i][j]);   //输入矩阵 
			}
		}
		bfs();
        printf("FINISH\n");
        //scanf("%d%d",&n,&m);
	}
	return 0;
}

拯救公主,从(0,0)出发,终点(n.m),有一个结构体保存步骤...............

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值