CSUST第13届校赛 G-逃离迷宫

版权声明:copy right https://blog.csdn.net/qq_43521140/article/details/89152714

Source

题目描述

给你一个n*m的图,地图上’.‘代表可以走的地方,而’#'代表陷阱不能走,
'P’代表人物位置,'K’代表钥匙,'E’代表出口。人物一个,钥匙有多个,
('K’的数量<=50)),出口一个,每个位置可以向(上,下,左,右)四个
方向走一格,花费一个单位时间,现在你需要花费最少的时间拿到钥匙
然后从迷宫的出口出去(若没有钥匙,则不能进入迷宫出口所在的格子)。

输入描述:
第一行一个整数T(T <= 50),代表数据的组数
接下来一行n,m(n<=500,m<=500),代表地图的行和列
接下来n行,每行一个长度为m的字符串,组成一个图。

输出描述:
如果可以出去,输出所花费的最少时间。
如果不能出去,输出一行"No solution"。

示例

Input

3
5 5
…P
##…E
K#…
##…

5 5
P…

…E…

…K
5 5
P#…E
.#.#.
.#.#.
.#.#.
…#K

Output

No solution
12
No solution

思路:以人为起点跑一遍全图,记录下到每个钥匙所耗费的时间;再以出口为起点跑一遍全图,每跑到一个钥匙就更新答案 ans = min(ans,d[now.y][now.x] + key[now.y][now.x]) 最后判断一下是否有解,再输出ans即可。

扫描二维码关注公众号,回复: 6519181 查看本文章

ACODE:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<utility>
#include<vector>
#include<map>
#include<set>
#define lson l , m , rt << 1
#define rson m+1 , r , rt << 1 | 1
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
const double pi = 3.1415926535;
const double eps = 1e-6;
const int MXN = 1e5 + 7;
const int MXM = 5e4 + 7;
const int MX = 500 + 7;
const int maxbit = 18;
const double val = pi/180.0;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1000000007;
struct node
{
	int x;
	int y;
};
int key[MX][MX];
int n,m;
int tox[] = {1,-1,0,0};
int toy[] = {0,0,-1,1};
int vis[MX][MX];
char mp[MX][MX];
int d[MX][MX];
int ans = INF;
int bfs1(node s)
{
	mem(vis,0);
	mem(d,0);
	vis[s.y][s.x] = 1;
	d[s.y][s.x] = 0;
	queue<node>q;
	q.push(s);
	int flag = 0;
	while(!q.empty())
	{
		node now = q.front();
		q.pop();
		if(mp[now.y][now.x] == 'K'){
			flag = 1;
			key[now.y][now.x] = d[now.y][now.x];
		}
		for(int i = 0;i < 4;++i)
		{
			int nx = now.x + tox[i];
			int ny = now.y + toy[i];
			if(nx < 0||nx >= m||ny < 0||ny >= n) continue;
			if(vis[ny][nx]) continue;
			if(mp[ny][nx] == '#'||mp[ny][nx] == 'E') continue;
			q.push({nx,ny});
			vis[ny][nx] = 1;
			d[ny][nx] = d[now.y][now.x] + 1;
		}
	}
	if(!flag) return 0;
	return 1;
}
void bfs2(node s)
{
	mem(d,0);
	mem(vis,0);
	vis[s.y][s.x] = 1;
	d[s.y][s.x] = 0;
	queue<node>q;
	q.push(s);
	while(!q.empty())
	{
		node now = q.front();
		q.pop();
		if(mp[now.y][now.x] == 'K'){
			ans = min(ans,d[now.y][now.x] + key[now.y][now.x]);
		}
		for(int i = 0;i < 4;++i)
		{
			int nx = now.x + tox[i];
			int ny = now.y + toy[i];
			if(nx < 0||nx >= m||ny < 0||ny >= n) continue;
			if(vis[ny][nx]) continue;
			if(mp[ny][nx] == '#') continue;
			q.push({nx,ny});
			vis[ny][nx] = 1;
			d[ny][nx] = d[now.y][now.x] + 1;
		}
	}
	return ;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		//忘了初始化WA一发 血亏
		ans = INF;
		mem(key,0);
		int flag = 0;
		node a;
		mem(vis,0);
		scanf("%d %d",&n,&m);
		for(int i = 0;i < n;++i) scanf("%s",mp[i]);
		for(int i = 0;i < n;++i)
		{
			for(int j = 0;j < m;++j)
			{
				if(mp[i][j] == 'P'){
					flag = bfs1({j,i});
				}
				if(mp[i][j] == 'E') 
				{
					a.x = j;
					a.y = i;
				}
			}
		}
		if(!flag){
			printf("No solution\n");
			continue;
		}
		bfs2(a);
		if(ans == INF){
			printf("No solution\n");
			continue;
		}
		printf("%d\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43521140/article/details/89152714
今日推荐