排位赛二G. Bucket Brigade

题目

给定一个10x10的地图,B是仓库,L是湖泊,’.'是路,R是石头(石头不能走),现在仓库着火了,母牛哥要去救火了,求仓库到湖泊的最小步数(仓库和湖泊不算入步数)

解法

bfs模板题。。。。。

代码

#include <iostream>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;

int _map[15][15];
bool vis[15][15];
int dir[4][2] = {1,0, 0,1, -1,0, 0,-1};
int sx, sy;
int gx, gy;
int main() {
	queue <int> xx, yy, ans;
	char ch;
	for(int i=1; i<=10; ++i) {
		for(int j=1; j<=10; ++j) {
			cin >> ch;
			if(ch=='B') {
				sx = i;
				sy = j;
				xx.push(i);
				yy.push(j);
				ans.push(-1);
				vis[i][j] = 1;
			}
			if(ch=='L') {
				gx = i;
				gy = j;
				_map[i][j] = 1;
			}
			if(ch=='.') {
				_map[i][j] = 1;
			}
		}
	}
	
	while(xx.size()) {
		int x = xx.front();
		int y = yy.front();
		int t = ans.front();
		if(x==gx && y==gy) {
			cout << t;
			break;
		}
		
		xx.pop();
		yy.pop();
		ans.pop();
		
		for(int k=0; k<4; ++k) {
			int tx = x + dir[k][0];
			int ty = y + dir[k][1];
			if(tx >0 && ty>0 && tx<=10 && ty<=10 && !vis[tx][ty] && _map[tx][ty]) {
				xx.push(tx);
				yy.push(ty);
				vis[tx][ty] = 1;
				ans.push(t+1);
			}
		}
	}

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值