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;
}

注意

处理好二分边界

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值