7-54 利比亚行动(宁波小学2011) (30分) bfs模板

2011年3月16日以来,利比亚爆发的骚乱不断升级,已严重危及到普通民众和各国在利比亚工作的人员的安全。为了尽快救出在利比亚的同胞,根据利比亚的形势,我国政府告诉每个在利比亚的公民,如何行动才能最快地到达安全的地方,然后由我国派出的飞机、轮船、汽车接回国。 假设利比亚的地图可以描述为一个n行m列的长方形,待拯救的同胞小A在1行1列处,安全的目标位置在n行m列处。小A每次只能向相邻的上、下、左、右四个方向移动,即如果小A现在的位置是i行j列,小A的下一步位置将到达i-1行j列、i+1行j列、i行j-1列、i行j+1列这四个位置之一,当然小A不能移出n行m列的长方形。 利比亚是一个多沙漠且地形复杂的国家,某些位置是很危险的,人不能去。 给出利比亚的地图,请告诉小A从起点(1,1)走到终点(n,m)最快需要多少步呢?
输入格式:

第一行有2个正整数n,m (1≤n≤2000,1≤m≤2000),它们之间以一个空格分隔,表示利比亚的地形可以分为n行m列。 接下来n行,每行m个字符,分别表示地图中该位置的信息。其中: 字符“”表示这个位置是建筑物、河流、有地雷等人无法走到的位置(保证起点终点不是“”); 小数点“.”表示人可以走到该位置。
输出格式:

只有一行,该行只有一个正整数。表示为小A从起点到终点,最快需要多少步。
输入样例:

3 5
.*...
...*.
*..*.

输出样例:

8
#define debug
#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;

char mtx[2048][2048];

struct Node {
	int r, c, step;
} ;

int dr[] = { 1, -1, 0, 0 };
int dc[] = { 0, 0, 1, -1 };

void bfs() {
	queue<Node> q;
	q.push({1, 1, 0});
	while(!q.empty()) {
		auto& no = q.front(); 
		mtx[no.r][no.c] = '*';
		if(no.r == n && no.c == m) {
			printf("%d\n", no.step);
			return ;
		}
		for(int i=0; i<4; i++) {
			int nr = no.r + dr[i], nc = no.c + dc[i];
			if(!mtx[nr][nc] || mtx[nr][nc]=='*') continue ;
			q.push({nr, nc, no.step+1});
			mtx[nr][nc] = '*';
		}
		q.pop();
	}
}

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);
	bfs();





#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、付费专栏及课程。

余额充值