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;
}
这篇博客介绍了G. Bucket Brigade问题,源自GDUT 2020寒假训练排位赛。问题要求确定在农场地图上形成有效水桶传递队列所需的最少奶牛数量,以扑灭火灾。地图由'B'(谷仓)、'L'(湖)和'R'(大岩石)等字符表示,奶牛只能在空地上相邻移动。解决方案采用了广度优先搜索(BFS)策略,从谷仓开始搜索直到到达湖,避免经过岩石。
438

被折叠的 条评论
为什么被折叠?



