本文主要是介绍Bucket Brigade//水题//排位2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Bucket Brigade//水题//排位2
题目
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: [Math Processing Error] 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.
Example
inputCopy
…
…
…
…B…
…
…R…
…
…
…L…
…
outputCopy
7
题意
从B到L,绕过R,求最短距离
思路
水题,特判三点在同一直线即可
代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <deque>
#include <queue>
#include <stack>
#define pi 3.1415926
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
char s[20][20];
int sx,sy,ex,ey,ans=999999;
int rx,ry;
int main(){memset(s,0,sizeof(s));for(int i=1;i<=10;i++)scanf("%s",s[i]+1);for(int i=1;i<=10;i++){for(int j=1;j<=10;j++){if(s[i][j]=='B'){sx=j;sy=i;}if(s[i][j]=='L'){ex=j;ey=i;}if(s[i][j]=='R'){rx=j;ry=i;}}}int d1=abs(sx-rx)+abs(sy-ry);int d2=abs(ex-rx)+abs(ey-ry);ans=abs(sx-ex)+abs(sy-ey);if((sx==ex||sy==ey)&&(ans==d1+d2))ans+=2;printf("%d\n",ans-1);return 0;
}
注意
处理好二分边界
这篇关于Bucket Brigade//水题//排位2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!