哈尔滨理工大学软件与微电子学院程序设计竞赛 C题 Coronavirus 基础bfs

链接:https://ac.nowcoder.com/acm/contest/5929/C
来源:牛客网

题目描述
众所周知,新型冠状病毒的传染性比较强。有一天多多想出家门买东西,但是从家到超市可能会经过高危地段,他想远远的避开这些高危地段并且走最少的路到达超市。
多多每次可以向上、下、左、右四个方向移动。如果一个地段高危,那么多多不会通过高危地段自身及其八个方向的地段。(高危地段自身以及上、下、左、右、左上、左下、右上、右下的地段都不能通过)
如果超市位于高危地段的八个方向内,那么多多不能到达超市。请问他能顺利到达超市吗?如果能的话输出最短路径长度,否则输出"Impossible"。
输入描述:

输入第一行包含两个正整数N,M (1 ≤ N ≤ 50,1 ≤ M ≤ 50)
接下来N行输入长度为M的字符串
字符’S’为家的位置,‘E’为超市的位置,’.‘代表可走地段,’*'代表高危地段

输出描述:

输出一个整数(最短路径长度)或字符串"Impossible"

示例1
输入
复制

3 3
*…

.SE

输出
复制

1

示例2
输入
复制

3 3
S…

*.E

输出
复制

4

示例3
输入
复制

3 3
S…

.*E

输出
复制

Impossible

b f s bfs bfs即可

#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long 
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
	do { \
		cout << "\033[31;1m " << #x << " -> "; \
		err(x); \
	} while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO {

	char print_f[105];
	void read() { }
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K, 
	sr/*起点行坐标*/,
	sc/*起点列坐标*/,
	er/*终点行坐标*/, 
	ec/*终点列坐标*/;
int dr[] = { 1, -1, 0, 0, 1, 1, -1, -1 };
int dc[] = { 0, 0, 1, -1, 1, -1, -1, 1 };

struct Node {
	int r, c, step/*step是当前走到mtx[r][c]的步数*/;
} ;

char mtx[128][128], vis[128][128];

void visit(int r, int c) { //将mtx[r][c]和相邻的8个方向设置为不可访问
	vis[r][c] = true;
	for(int i=0; i<8; i++)
		vis[r+dr[i]][c+dc[i]] = true;
}

int bfs() {
	queue<Node> q;
	q.push({sr, sc, 0});
	while(!q.empty()) {
		auto no = q.front(); q.pop();
		if(no.r==er && no.c==ec) return no.step;
		for(int i=0; i<4; i++) {
			int nr = no.r+dr[i], nc = no.c+dc[i];
			if(mtx[nr][nc] && !vis[nr][nc]) { //因为我的下标从[1,1]开始,所以越界的值都是0(全局变量自动赋值为0)
				q.push({nr, nc, no.step+1});
				vis[nr][nc] = true;
			}
		}
	}
	return -1;
}

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	// freopen("out_main", "w", stdout);
	clock_t stime = clock();
#endif
	scanf("%d %d ", &n, &m);
	for(int i=1; i<=n; i++) 
		scanf("%s ", mtx[i]+1);
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++) {
			if(mtx[i][j] == '*') visit(i, j);
			if(mtx[i][j] == 'S') sr = i, sc = j;
			if(mtx[i][j] == 'E') er = i, ec = j;
		}
	int ret = bfs();
	if(~ret) 
		printf("%d\n", ret);
	else
		printf("Impossible\n");


#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值