G. Bucket Brigade

GDUT 2020寒假训练 排位赛二 G

原题链接

题目

原题截图
A fire has broken out on the farm, and the cows are rushing to try and put it out! The farm is described by a 10×10 grid of characters like this:
示例
The character ‘B’ represents the barn, which has just caught on fire. The ‘L’ character represents a lake, and ‘R’ represents the location of a large rock.

The cows want to form a “bucket brigade” by placing themselves along a path between the lake and the barn so that they can pass buckets of water along the path to help extinguish the fire. A bucket can move between cows if they are immediately adjacent in the north, south, east, or west directions. The same is true for a cow next to the lake — the cow can only extract a bucket of water from the lake if she is immediately adjacent to the lake. Similarly, a cow can only throw a bucket of water on the barn if she is immediately adjacent to the barn.

Please help determine the minimum number of ‘.’ squares that should be occupied by cows to form a successful bucket brigade.

A cow cannot be placed on the square containing the large rock, and the barn and lake are guaranteed not to be immediately adjacent to each-other.

Input
The input file contains 10 rows each with 10 characters, describing the layout of the farm.

Output
Output a single integer giving the minimum number of cows needed to form a viable bucket brigade.

样例
input
..........
..........
..........
..B.......
..........
.....R....
..........
..........
.....L....
..........
output
7
题目大意

从B出发到L,其中R不能走,我们可以沿着上下左右四个方向走,求最少的步数。

思路

广搜
纯广搜,从B点开始四个方向搜索加入到open表中,并将b点设置为不可走,然后从open表中取出第一个点再走,知道到达了目标地点,输出步数即可。

代码

#include<iostream>
#include<cstdio>
#include<memory.h>
#include<queue>
#include<set>
using namespace std;
char mp[20][20];
bool check[20][20];
int order[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
struct POINT{
	int x,y;
	int step;
};
queue<POINT> open;
//set<POINT> close;
int main()
{
	POINT st,ed;
	memset(check,false,sizeof(check));
	for(int i=0;i<10;i++)
	{
		scanf("%s",mp[i]);
		for(int j=0;j<10;j++)
		{
			if(mp[i][j]=='B')//找到起点 
			{
				st.x=i;
				st.y=j;
				st.step=0;
			}
			if(mp[i][j]=='L')//找到终点 
			{
				ed.x=i;
				ed.y=j;
			}
		}
	}
	//cout<<st.x<<" "<<st.y<<" "<<ed.x<<" "<<ed.y<<endl;
	open.push(st);
	check[st.x][st.y]=1;//标记已经走过 
	POINT noww,next;
	while(!open.empty())
	{
		noww=open.front();
		open.pop();
		if(noww.x==ed.x&&noww.y==ed.y)
		{
			cout<<noww.step-1<<endl;
			return 0;
		}
		//close.insert(noww);
		for(int i=0;i<4;i++)
		{
			next.x=noww.x+order[i][0];
			next.y=noww.y+order[i][1];
			if(check[next.x][next.y]==false&&mp[next.x][next.y]!='R'&&(next.x>=0&&next.x<10)&&(next.y>=0&&next.y<10))
			{
				next.step=noww.step+1;
				check[next.x][next.y]=1;//标记已经走过 
				open.push(next);
			}
		}
	}
	
	
	return 0;
}
发布了21 篇原创文章 · 获赞 1 · 访问量 420

猜你喜欢

转载自blog.csdn.net/xcy2001/article/details/104470047