OpenJ_Bailian-4116. 拯救公主

本文介绍了一种迷宫救援游戏的算法实现,游戏目标为骑士营救被囚禁的公主。通过两种不同的方法实现了最短路径寻找:一是利用优先队列进行时间排序,二是采用普通队列对守卫进行特殊处理。文章提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面如下:

 

公主被恶人抓走,被关押在牢房的某个地方。牢房用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有个难点就是要打败守卫要消耗时间多,所以第一个想法就是使用优先队列来对时间进行排序,来找到最短时间,第二个想法还是使用普通对列对守卫进行特殊处理.

代码一,使用优先队列:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>

using namespace std;
typedef long long ll;
const ll maxn=1e7+10;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int s;
int n,m;
int Map[210][210];
bool vis[210][210];
char a[210][210];
int gx,gy;
struct node{
	int x,y;
	int time;
	friend bool operator <(const node A,const node B){
		return A.time>B.time;
	}
};
priority_queue<node>q;
int main()
{
	ios::sync_with_stdio(false);
	cin>>s;
	while(s--){
		while(!q.empty())q.pop();
		memset(vis,false,sizeof(vis));
		memset(Map,0,sizeof(Map));
		cin>>n>>m;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				cin>>a[i][j];
				if(a[i][j]=='x')Map[i][j]=1;
				else if(a[i][j]=='#')Map[i][j]=2;
				else if(a[i][j]=='r')q.push(node{i,j,0}),vis[i][j]=true;
				else if(a[i][j]=='a')gx=i,gy=j;
				
			}
		}
		while(!q.empty()){
			int x=q.top().x,y=q.top().y,time=q.top().time;
			if(x==gx&&y==gy)break;
			for(int i=0;i<4;i++){
				int tx=x+dx[i],ty=y+dy[i];
				if(tx>=0&&tx<n&&ty>=0&&ty<m&&!vis[tx][ty]&&Map[tx][ty]!=2){
					vis[tx][ty]=true;
					if(Map[tx][ty]==0)q.push(node{tx,ty,time+1});
					else if(Map[tx][ty]==1)q.push(node{tx,ty,time+2});
				}
			}
			q.pop();
		}
		if(q.empty())cout<<"Impossible"<<endl;
		else cout<<q.top().time<<endl;
	}
 	return 0;
}

代码二,使用普通队列:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>

using namespace std;
typedef long long ll;
const ll maxn=1e7+10;
const int INF=1<<30;
int n,m;
int s;
char a[210][210];
int g[210][210];
int sx,sy;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
struct node{
	int x,y;
	int val;
};
int ans;
void bfs(){
	queue<node>p; 
	p.push(node{sx,sy,0});
	ans =INF;
	while(!p.empty()){
		node h=p.front();
		p.pop();
		if(a[h.x][h.y]=='a'){
			ans=min(ans,h.val);
			return ;
		}
		if(a[h.x][h.y]=='x'&&g[h.x][h.y]==1){
			p.push(node{h.x,h.y,h.val+1});
			g[h.x][h.y]=0;
		}else{
			for(int i=0;i<4;i++){
				node t;
				t.x=h.x+dx[i];
				t.y=h.y+dy[i];
				t.val=h.val;
				if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&g[t.x][t.y]){
					if(a[t.x][t.y]=='x'&&g[t.x][t.y]==2){
						p.push(node{t.x,t.y,t.val+1});
						g[t.x][t.y]=1;
					}else if(a[t.x][t.y]!='x'){
						p.push(node{t.x,t.y,t.val+g[t.x][t.y]});
						g[t.x][t.y]=0;
					}
				}
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>s;
	while(s--){
		cin>>n>>m;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				cin>>a[i][j];
				if(a[i][j]=='x')g[i][j]=2;
				else if(a[i][j]=='r')sx=i,sy=j,g[i][j]=0;
				else if(a[i][j]=='@'||a[i][j]=='a')g[i][j]=1;
				else g[i][j]=0;
			}
		}
		bfs();
		if(ans==INF)cout<<"Impossible"<<endl;
		else cout<<ans<<endl;
	}
 	return 0;
}
### 下载指定版本的 OpenJDK 11 开发包 为了从阿里云镜像站点下载 CentOS 7.9 中 `java-11-openjdk-devel-11.0.23.0.9-2.el7_9.x86_64.rpm` 包,可以按照如下方法操作: #### 使用命令行工具 wget 或 curl 进行下载 通过访问阿里云的 CentOS 镜像源地址并定位到具体的 RPM 文件路径来获取所需软件包。 ```bash wget http://mirrors.aliyun.com/centos/7.9.2009/AppStream/x86_64/os/Packages/java-11-openjdk-devel-11.0.23.0.9-2.el7_9.x86_64.rpm ``` 或者使用 `curl` 命令: ```bash curl -O http://mirrors.aliyun.com/centos/7.9.2009/AppStream/x86_64/os/Packages/java-11-openjdk-devel-11.0.23.0.9-2.el7_9.x86_64.rpm ``` 上述 URL 是基于阿里云提供的 CentOS 7.9 的 AppStream 存储库中的位置构建而成。如果该链接不可用,则可能是因为存储库结构有所变化或是版本更新所致[^1]。 #### 安装已下载的 RPM 包 一旦成功下载了所需的 `.rpm` 文件之后,可以通过以下命令来进行本地安装: ```bash sudo rpm -ivh java-11-openjdk-devel-11.0.23.0.9-2.el7_9.x86_64.rpm ``` 这将会把 Java Development Kit (JDK) 版本 11 安装至系统上,并设置好环境变量以便于后续开发工作[^2]。 #### 设置 JAVA_HOME 环境变量 完成 JDK 的安装后,建议配置 `JAVA_HOME` 变量指向新安装的 JDK 路径。编辑 `/etc/profile.d/javahome.sh` 文件,在其中添加相应的出口声明语句以确保每次登录时自动加载此配置项。 ```bash export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.23.0.9-2.el7_9.x86_64/ export PATH=$PATH:$JAVA_HOME/bin ``` 保存更改后的文件并通过执行 source 命令使这些修改立即生效: ```bash source /etc/profile.d/javahome.sh ``` 这样就完成了从阿里云镜像站下载并安装特定版本的 OpenJDK 11 开发包的过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值