P1699 [USACO19OPEN] Bucket Brigade B

题意&思路:

这题花里胡哨的,简化题意一句话:B到L最少的步数,R处不能走。
那么就只需要记录起点、终点位置,套BFS模板就行了。

Code

#include <bits/stdc++.h>
using namespace std;
char a[150][150];
long long v[150][150];
struct point{
	long long x1,y1,step;
}start;
long long dx[4] = {1,0,-1,0};
long long dy[4] = {0,1,0,-1};
queue<point> dl;
int main(){
	long long n,m;
	for(long long i=1;i<=10;i++){
		for(long long j=1;j<=10;j++){
			cin >> a[i][j];
			if(a[i][j]=='B'){
				start.x1 = i;
				start.y1 = j;
			}
			if(a[i][j]=='L'){
				n=i;
				m=j;
			}
		}
	}
//	for(int i=1; i<=10; i++)cout<<a[i]<<endl;
//	cout<<n<<' '<<m<<endl;
	start.step = 1;
	v[start.x1][start.y1] = 1;
	dl.push(start);
	while(!dl.empty()){
		point head = dl.front();
		dl.pop();
		if(head.x1 == n && head.y1 == m){
			cout << head.step-2 << endl;
			break;
		}
//		cout << head.x1 << ' ' << head.y1 << ' ' << head.step<<endl;
		for(long long i=0;i<4;i++){
			long long tx = head.x1 + dx[i];
			long long ty = head.y1 + dy[i];
			if(tx <= 10 && tx > 0 && ty <= 10 && ty > 0 && v[tx][ty]==0 && a[tx][ty]=='.' || a[tx][ty]=='L'){
				v[tx][ty] = 1;
				point temp;
				temp.x1 = tx;
				temp.y1 = ty;
				temp.step = head.step + 1;
				dl.push(temp);
			}
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值