HDU 1026 Ignatius and the Princess I 优先队列 +BFS.

这个国庆一直在做搜索题,感觉头有点痛。
今天学了一下优先队列 不知道优先队列之前我以为用宽搜做不出来(可见我有多LOW,最基础的优先队列都不知道 o(︶︿︶)o )。写了半天的深搜,样例也出不了。搜了一下题解,发现就是写过很多遍的BFS,然后从终点回溯路径到起点,再倒过来输出。但是一开始还是想不通为什么会能够自动算得最小的时间,而不是最少的步数。 于是百度了下  priority_queue 。优先队列就是可以让  数据保存在队列的时候按想要的顺序排好,那么弹出队列的时候也就是最大或最小的了。

就是这句

struct node
{
	int x,y;
	int step;
	friend bool operator<(node n1,node n2)   //就是这句、
	{
		return n2.step<n1.step;
	}
}rec[105][105];


另外的话,输出路径,搜的题解用的是递归,我自己以前学过一种换一个数组再拷贝一遍从尾输出。
感觉还可以从终点往起点搜。这样的话保存的路径的顺序应该就是从起点指向终点。


#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;

struct node
{
	int x,y;
	int step;
	friend bool operator<(node n1,node n2)
	{
		return n2.step<n1.step;
	}
}rec[105][105];
int dir[4][2]={0,1, 1,0, 0,-1, -1,0};
int map[111][111];
int flag[111][111];
int blood[111][111];
int n,m;

int judge(int x,int y)
{
	if(x<0 || x>=n || y<0 || y>=m)	return 1;
	if(map[x][y]==-1)				return 1;
	return 0;
}
int BFS()
{
	priority_queue<node>q;
	node cur,next;
	int i;

	cur.x=0;
	cur.y=0;
	cur.step=0;
	map[0][0]=-1;
	q.push(cur);
	while(!q.empty())
	{
		cur=q.top();
		q.pop();
		if(cur.x==n-1 && cur.y==m-1)	return cur.step;
		for(i=0;i<4;i++)
		{
			next.x=cur.x+dir[i][0];
			next.y=cur.y+dir[i][1];

			if(judge(next.x,next.y))	continue;
			next.step=cur.step+1+map[next.x][next.y];
		//	flag[next.x][next.y]=i+1;
			rec[next.x][next.y].x=cur.x;
			rec[next.x][next.y].y=cur.y;
			map[next.x][next.y]=-1;
			q.push(next);
		}
	}
	return -1;
}
int temp;
void print()
{
    struct node p[10001];
    int i=n-1,j=m-1,a,b,k=1;
    p[0].x=n-1,p[0].y=m-1;
    while(i || j)
    {
        a=i;
        b=j;
        i=rec[a][b].x;
        j=rec[a][b].y;
        p[k].x=i;
        p[k++].y=j;
    }
    for(i=k-1;i>0;i--)
    {

       while (blood[p[i].x][p[i].y]--) { printf("%ds:FIGHT AT (%d,%d)\n",temp++,p[i].x,p[i].y); }
       printf("%ds:(%d,%d)->(%d,%d)\n",temp++,p[i].x,p[i].y,p[i-1].x,p[i-1].y);
    }
    while (blood[p[i].x][p[i].y]--) { printf("%ds:FIGHT AT (%d,%d)\n",temp++,p[i].x,p[i].y);}
}
/*
int temp;
void P(int x,int y)
{
	int next_x,next_y;
	if(flag[x][y]==0)	return ;
	next_x=x-dir[flag[x][y]-1][0];
	next_y=y-dir[flag[x][y]-1][1];
	P(next_x,next_y);
	printf("%ds:(%d,%d)->(%d,%d)\n",temp++,next_x,next_y,x,y);

	while(blood[x][y]--)	printf("%ds:FIGHT AT (%d,%d)\n",temp++,x,y);
}
*/
int main()
{
	char str[111];
	int i,l;
	int ans;

	while(scanf("%d%d",&n,&m)!=-1)
	{
		memset(map,0,sizeof(map));
		memset(flag,0,sizeof(flag));
		memset(blood,0,sizeof(blood));
		for(i=0;i<n;i++)
		{
			scanf("%s",str);
			for(l=0;str[l];l++)
			{
				if(str[l]=='.')		map[i][l]=0;
				else if(str[l]=='X')map[i][l]=-1;
				else				map[i][l]=blood[i][l]=str[l]-'0';
			}
		}
		ans=BFS();
		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);

			temp=1;
			//P(n-1,m-1);
			print();
		}
		printf("FINISH\n");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值