22搜索专题 F HDU 1242 Rescue (优先队列)

 

 题目意思: 起点是a,终点是r,#不可走,x可走但是要多花费一单位时间,问你从a到r的最短时间是多少。

思路:优先队列板子(超级简单的好吧),就是使用优先队列,然后每次拿出时间一致的进行搜索,如果找到该点就直接输出当前的时间即可。

注意优先队列的一个排序函数,因为我优先队列里面用的是结构体,所以要写个自定义函数用来判断排序条件:

bool  operator<(lk a,lk b)

{

    return a.cnt  > b.cnt;

}

其他的也没什么好讲的,就是要注意如果到不了的话的输出后面是带了逗号的(麻麻的wa了) 。

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃  	    ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long 
using namespace std;

const int N=1000000+100;
int n ,m,h;
// ll s[N];
char ant[220][220];
int dx[5]={0,0,1,0,-1};
int dy[5]={0,1,0,-1,0};
bool st[220][220];

struct lk{
	int x;
	int y;
	int cnt;
};

bool  operator<(lk a,lk b)
{
    return a.cnt  > b.cnt;
}

void bfs(int x,int y)
{
	priority_queue<lk>q;
	st[x][y]=1;	
	q.push({x,y,0});
	while(q.size())
	{
		lk k=q.top();
		q.pop();
		if(ant[k.x][k.y]=='a'){
			cout<<k.cnt<<endl;
			return ;
		}

		for(int i =1;i<=4;i++)
		{
			int x=k.x+dx[i],y=k.y+dy[i];
			if(x<1||x>n||y<1||y>m)continue;
			if(st[x][y]||ant[x][y]=='#')continue;
			st[x][y]=1;
			if(ant[x][y]=='x')
			q.push({x,y,k.cnt+2});
			else 
			q.push({x,y,k.cnt+1});
		}
	}
	cout<<"Poor ANGEL has to stay in the prison all his life.\n";
	return ;
}

int main()
{
	int t;
	while(~scanf("%d%d",&n,&m))
	{
		if(n==0&&m==0)break;
		memset(st,0,sizeof st);
		int xx,yy;
		for(int i =1;i<=n;i++)
		{
			cin>>ant[i]+1;
			for(int j =1;j<=m;j++)
			if(ant[i][j]=='r'){
				xx=i,yy=j;
			}
		}

		bfs(xx,yy);

	}
	

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值