拯救计划 计蒜客--1213 bfs+优先队列

题目链接
题目如下:

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

输入格式:

1、两个整数代表 N N N M , ( N , M ≤ 200 ) M, (N, M \le 200) M,(N,M200).
2、随后 N N N 行,每行有 M M M 个字符。"@" 代表道路,“a” 代表公主,“r” 代表骑士,“x” 代表守卫, “#” 代表墙壁。

输出格式:

如果拯救行动成功,输出一个整数,表示行动的最短时间。
如果不可能成功,输出 “Impossible”。

Sample Input:
7 8
#@#####@
#@a#@@r@
#@@#x@@@
@@#@@#@#
#@@@##@@
@#@@@@@@
@@@@@@@@
Sample Output

13

Sample Input 2
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 2
7

这个题要使用快排或者优先队列去写,我先上传的代码是坐标形式的,每从一个点向外延深结束就要根据时间进行一次排序。

快排代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
struct muban
{
 	int x;
 	int y;
 	int time;
}que[50000];
char imap[205][205];
int book[205][205];
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool cmp(muban a,muban b)
{
 	return a.time<b.time;
}
int main()
{
 	int n,m;
 	cin>>n>>m;
 	int stax,stay,endx,endy;
 	for(int i=0;i<n;i++)
 	{
  		for(int j=0;j<m;j++)
  		{
   			cin>>imap[i][j];
   			if(imap[i][j]=='r')
   			{
   				stax=i;stay=j;
   			}
   			else if(imap[i][j]=='a')
   			{
    				endx=i;endy=j;
    				imap[i][j]='@';
   			}
  		}
 	}
 	int head,tail,tx,ty;
 	head=1;
 	tail=2;
 	que[head].x=stax;
 	que[head].y=stay;
 	que[head].time=0;
 	int flag=0;
 	while(head<tail)
 	{
  		for(int i=0;i<4;i++)
  		{
   			tx=next[i][0]+que[head].x;
   			ty=next[i][1]+que[head].y;
   			if(tx>=0&&tx<n&&ty>=0&&ty<m)
   			{
    				if(imap[tx][ty]=='@'&&book[tx][ty]==0)
    				{
     					book[tx][ty]=1;
     					que[tail].x=tx;
     					que[tail].y=ty;
     					que[tail].time=que[head].time+1;
     					tail++;
    				}
    				else if(imap[tx][ty]=='x'&&book[tx][ty]==0)
    				{
     					book[tx][ty]=1;
     					que[tail].x=tx;
     					que[tail].y=ty;
     					que[tail].time=que[head].time+2;
     					tail++;
    				}
   			}
   			if(que[tail-1].x==endx&&que[tail-1].y==endy)
   			{
    				flag=1;
    				break;
   			}
  		}
  		if(flag==1)
  		break;
  		head++;
  		sort(que+head,que+tail,cmp);
 	}
 	if(flag==0)
 	cout<<"Impossible"<<endl;
 	else
 	cout<<que[tail-1].time<<endl;
 	return 0;
}

优先队列代码如下:

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct muban
{
	int x;
	int y;
	int time;
	muban (int xx,int yy,int ttime)
	{
		x=xx;
		y=yy;
		time=ttime;
	} 
	friend bool operator <(muban a,muban b)
	{
		return a.time>b.time;
	}
};
char imap[205][205];
int book[205][205];
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int main()
{
	int n,m;
	int stax,stay,endx,endy;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>imap[i][j];
			if(imap[i][j]=='r')
			{
				stax=i;
				stay=j;
			}
			if(imap[i][j]=='a')
			{
				endx=i;
				endy=j;
			}
		}
	}
	priority_queue<muban> q;
	muban temp(stax,stay,0);
	book[stax][stay]=1;
	q.push(temp);
	int tx,ty,flag;
	flag=0;
	int sumtime;
	while(q.empty()==0)
	{
		temp=q.top();
		q.pop();
		for(int i=0;i<4;i++)
		{
			tx=temp.x+next[i][0];
			ty=temp.y+next[i][1];
			if(tx>=0&&tx<n&&ty>=0&&ty<m)
			{
				if(book[tx][ty]==0&&imap[tx][ty]=='@')
				{
					q.push(muban(tx,ty,temp.time+1));
					book[tx][ty]=1;
				}
				if(book[tx][ty]==0&&imap[tx][ty]=='x')
				{
					q.push(muban(tx,ty,temp.time+2));
					book[tx][ty]=1;
				}
				if(book[tx][ty]==0&&imap[tx][ty]=='a')
				{
					flag=1;
					sumtime=temp.time+1;
					break;
				}
			}
		}
		if(flag==1)
		break;
	}
	if(flag==0)
	cout<<"Impossible"<<endl;
	else
	cout<<sumtime<<endl;
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值