4980:拯救行动 题解

4980:拯救行动

——————————————————————

总时间限制: 1000ms 内存限制: 65536kB

描述

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

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

输入

第一行为一个整数S,表示输入的数据的组数(多组输入)
随后有S组数据,每组数据按如下格式输入
1、两个整数代表N和M, (N, M <= 200).
2、随后N行,每行有M个字符。"@"代表道路,"a"代表公主,"r"代表骑士,"x"代表守卫, "#"代表墙壁。

输出

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

样例输入

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#####@

样例输出

13
7
听说在NOI Online前发题解会RP+=0x7f7f7f7f

我是个初学C++的小蒟蒻,目前只发现了一种办法:
那就是:

BFS!BFS!BFS!!!!!!!!!

queue+struct 即可

tip: 经过守卫所花费的时间是不一样的!!!!!!!!

不多说了,贴代码:

#include<bits/stdc++.h>
#define R register
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;
//快读
template <typename T> void read(T &t){
	t=0;
	char ch=getchar();
	int f=1;
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	do{
		(t*=10)+=ch-'0';
		ch=getchar();
	}while(ch>='0'&&ch<='9');
	t*=f;
}
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(){
    //减去输入输出流的多余时间,使其的速度与scanf及printf的速度一致
	ios::sync_with_stdio(false);
	read(s);
	while(s--){
		read(n);
		read(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;
}

代码参考: https://blog.csdn.net/ab1605014317/article/details/81476482

她透过我的血,看到了另一抹殷红
原文地址:https://www.cnblogs.com/zhangbeini/p/13771266.html