拯救行动 【优先队列BFS】

公主被恶人抓走,被关押在牢房的某个地方。牢房用N*M (N, M <= 200)的矩阵来表示。矩阵中的每项可以代表道路(@)、墙壁(#)、和守卫(x)。
英勇的骑士(r)决定孤身一人去拯救公主(a)。我们假设拯救成功的表示是“骑士到达了公主所在的位置”。由于在通往公主所在位置的道路中可能遇到守卫,骑士一旦遇到守卫,必须杀死守卫才能继续前进。
现假设骑士可以向上、下、左、右四个方向移动,每移动一个位置需要1个单位时间,杀死一个守卫需要花费额外的1个单位时间。同时假设骑士足够强壮,有能力杀死所有的守卫。

给定牢房矩阵,公主、骑士和守卫在矩阵中的位置,请你计算拯救行动成功需要花费最短时间。

Input
第一行为一个整数S,表示输入的数据的组数(多组输入)
随后有S组数据,每组数据按如下格式输入
1、两个整数代表N和M, (N, M <= 200).
2、随后N行,每行有M个字符。"@"代表道路,"a"代表公主,"r"代表骑士,"x"代表守卫, "#“代表墙壁。
Output
如果拯救行动成功,输出一个整数,表示行动的最短时间。
如果不可能成功,输出"Impossible”
Sample Input
2
7 8
#@#####@
#@a#@@r@
#@@#x@@@
@@#@@#@#
#@@@##@@
@#@@@@@@
@@@@@@@@
13 40
@x@@##x@#x@x#xxxx##@#x@x@@#x#@#x#@@x@#@x
xx###x@x#@@##xx@@@#@x@@#x@xxx@@#x@#x@@x@
#@x#@x#x#@@##@@x#@xx#xxx@@x##@@@#@x@@x@x
@##x@@@x#xx#@@#xxxx#@@x@x@#@x@@@x@#@#x@#
@#xxxxx##@@x##x@xxx@@#x@x####@@@x#x##@#@
#xxx#@#x##xxxx@@#xx@@@x@xxx#@#xxx@x#####
#x@xxxx#@x@@@@##@x#xx#xxx@#xx#@#####x#@x
xx##@#@x##x##x#@x#@a#xx@##@#@##xx@#@@x@x
x#x#@x@#x#@##@xrx@x#xxxx@##x##xx#@#x@xx@
#x@@#@###x##x@x#@@#@@x@x@@xx@@@@##@@x@@x
x#xx@x###@xxx#@#x#@@###@#@##@x#@x@#@@#@@
#@#x@x#x#x###@x@@xxx####x@x##@x####xx#@x
#x#@x#x######@@#x@#xxxx#xx@@@#xx#x#####@
Sample Output
13
7

解题思路:因为是求行动的最短时间,而不是距离,所以直接的普通BFS是不行的,因为距离最短但是路上守卫多,那么浪费时间也肯定更多,即最短距离不代表最短时间。
方案一: 全部搜索一下,利用一个全局最短时间变量,然后不断的进行min。
方案二:优先队列,每次按照同一层时间更短的进行入队、出队。

//核心就是把普通的BFS,改写成优先队列即可。
#include <iostream>
#include <queue>
#include <string.h>
//优先队列,因为距离最短,不一定时间最短,所以用优先队列,优先处理时间短的点 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int s;
int n,m;  //行,列
char map[200][200]; 
bool st[200][200];
struct node{
	int x,y,step;
}S,T,Node;
int fx[4]={-1,1,0,0};
int fy[4]={0,0,-1,1};
int minans=1<<30;

struct cmp{
	bool operator() (node a,node b){
		return a.step > b.step;
	}
};

bool judge(int x,int y){
	if(x< 0 || x>=n || y<0 || y>=m) return false;
	if(map[x][y] == '#') return false;
	if(st[x][y] == true)  return false;
	return true;
}

int bfs(){
	priority_queue<node,vector<node>,cmp> q;
	q.push(S);
	st[S.x][S.y] =true;
	while(!q.empty())
	{
		node tp=q.top();
		q.pop();
		if(tp.x == T.x && tp.y == T.y)
		{
			return tp.step;
		}
		for(int i=0;i<4;i++)
		{
			int newX=tp.x+fx[i];
			int newY=tp.y+fy[i];
			if(judge(newX,newY))
			{
				Node.x=newX;
				Node.y=newY;
				Node.step=tp.step+1;
				if(map[newX][newY] == 'x')
				Node.step+=1;
				q.push(Node);
				st[newX][newY] = true;
			}
		}
	}
	return -1;
} 
int main(int argc, char** argv) {
	cin >> s;
	while(s--)
	{
		cin >> n >> m;
		memset(map,'\0',sizeof(map));
		memset(st,false,sizeof(st));
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				cin >> map[i][j];
				if(map[i][j] == 'r')
				{
					S.x=i;
					S.y=j;
					S.step=0;
				}
				if( map[i][j] == 'a')
				{
					T.x=i;
					T.y=j;
				}
			}	
		}
        int t=bfs();
        if(t==-1)
        cout << "Impossible" << endl;
        else
        cout << t << endl;		
	}
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值