CF #542 Div2 C(BFS)

C. Connect

http://codeforces.com/contest/1130/problem/C

Alice lives on a flat planet that can be modeled as a square grid of size n×nn×n, with rows and columns enumerated from 11 to nn. We represent the cell at the intersection of row rr and column cc with ordered pair (r,c)(r,c). Each cell in the grid is either land or water.

An example planet with n=5n=5. It also appears in the first sample test.

Alice resides in land cell (r1,c1)(r1,c1). She wishes to travel to land cell (r2,c2)(r2,c2). At any moment, she may move to one of the cells adjacent to where she is—in one of the four directions (i.e., up, down, left, or right).

Unfortunately, Alice cannot swim, and there is no viable transportation means other than by foot (i.e., she can walk only on land). As a result, Alice's trip may be impossible.

To help Alice, you plan to create at most one tunnel between some two land cells. The tunnel will allow Alice to freely travel between the two endpoints. Indeed, creating a tunnel is a lot of effort: the cost of creating a tunnel between cells (rs,cs)(rs,cs) and (rt,ct)(rt,ct) is (rs−rt)2+(cs−ct)2(rs−rt)2+(cs−ct)2.

For now, your task is to find the minimum possible cost of creating at most one tunnel so that Alice could travel from (r1,c1)(r1,c1) to (r2,c2)(r2,c2). If no tunnel needs to be created, the cost is 00.

Input

The first line contains one integer nn (1≤n≤501≤n≤50) — the width of the square grid.

The second line contains two space-separated integers r1r1 and c1c1 (1≤r1,c1≤n1≤r1,c1≤n) — denoting the cell where Alice resides.

The third line contains two space-separated integers r2r2 and c2c2 (1≤r2,c2≤n1≤r2,c2≤n) — denoting the cell to which Alice wishes to travel.

Each of the following nn lines contains a string of nn characters. The jj-th character of the ii-th such line (1≤i,j≤n1≤i,j≤n) is 0 if (i,j)(i,j) is land or 1 if (i,j)(i,j) is water.

It is guaranteed that (r1,c1)(r1,c1) and (r2,c2)(r2,c2) are land.

Output

Print an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from (r1,c1)(r1,c1) to (r2,c2)(r2,c2).

Examples

input

Copy

5
1 1
5 5
00001
11111
00111
00110
00110

output

Copy

10

input

Copy

3
1 3
3 1
010
101
010

output

Copy

8

Note

In the first sample, a tunnel between cells (1,4)(1,4) and (4,5)(4,5) should be created. The cost of doing so is (1−4)2+(4−5)2=10(1−4)2+(4−5)2=10, which is optimal. This way, Alice could walk from (1,1)(1,1) to (1,4)(1,4), use the tunnel from (1,4)(1,4) to (4,5)(4,5), and lastly walk from (4,5)(4,5) to (5,5)(5,5).

In the second sample, clearly a tunnel between cells (1,3)(1,3) and (3,1)(3,1) needs to be created. The cost of doing so is (1−3)2+(3−1)2=8(1−3)2+(3−1)2=8.

题意:在陆地部分之间架一座桥,给定起点和终点位置,求这座使起点、终点连通的桥的最小花费。

分析:比较容易想到的BFS,DFS都可以过。起点和终点分别BFS一次,暴力枚举起点和终点各自的连通块中每两个格子间造桥的花费,取最小值。这道题听说也可以用并查集,所以虽然很水但感觉很高级。一开始没把起点终点的位置坐标-1就代入map所以出错了,不过也蛮容易发现的。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

char map[55][55];
int vst[55][55];
int n;

struct Point{
    int x,y;
};


int spd(int x11, int y11, int x22, int y22){
    return (x11-x22)*(x11-x22)+(y11-y22)*(y11-y22);
}

int bfs(Point begin, Point end){
    begin.x--;
    begin.y--;
    end.x--;
    end.y--;
    queue<Point> q1,q2;
    vector<Point> v1,v2;
    memset(vst,0,sizeof(vst));
    vst[begin.x][begin.y]=1;
    q1.push(begin);
    while(!q1.empty()){
        Point x=q1.front(),y;
        v1.push_back(q1.front());
        q1.pop();
        if(x.x-1>=0&&!vst[x.x-1][x.y]&&map[x.x-1][x.y]=='0'){
            vst[x.x-1][x.y]=1;
            y.x = x.x-1;
            y.y = x.y;
            q1.push(y);
        }
        if(x.x+1<n&&!vst[x.x+1][x.y]&&map[x.x+1][x.y]=='0'){
            vst[x.x+1][x.y]=1;
            y.x = x.x+1;
            y.y = x.y;
            q1.push(y);
        }
        if(x.y-1>=0&&!vst[x.x][x.y-1]&&map[x.x][x.y-1]=='0'){
            vst[x.x][x.y-1]=1;
            y.x = x.x;
            y.y = x.y-1;
            q1.push(y);
        }
        if(x.y+1<n&&!vst[x.x][x.y+1]&&map[x.x][x.y+1]=='0'){
            vst[x.x][x.y+1]=1;
            y.x = x.x;
            y.y = x.y+1;
            q1.push(y);
        }
    }
    memset(vst,0,sizeof(vst));
    vst[end.x][end.y]=1;
    q2.push(end);
    while(!q2.empty()){
        Point x=q2.front(),y;
        v2.push_back(q2.front());
        q2.pop();
        if(x.x-1>=0&&!vst[x.x-1][x.y]&&map[x.x-1][x.y]=='0'){
            vst[x.x-1][x.y]=1;
            y.x = x.x-1;
            y.y = x.y;
            q2.push(y);
        }
        if(x.x+1<n&&!vst[x.x+1][x.y]&&map[x.x+1][x.y]=='0'){
            vst[x.x+1][x.y]=1;
            y.x = x.x+1;
            y.y = x.y;
            q2.push(y);
        }
        if(x.y-1>=0&&!vst[x.x][x.y-1]&&map[x.x][x.y-1]=='0'){
            vst[x.x][x.y-1]=1;
            y.x = x.x;
            y.y = x.y-1;
            q2.push(y);
        }
        if(x.y+1<n&&!vst[x.x][x.y+1]&&map[x.x][x.y+1]=='0'){
            vst[x.x][x.y+1]=1;
            y.x = x.x;
            y.y = x.y+1;
            q2.push(y);
        }
    }
    int ans=100005;
    for(vector<Point>::iterator i=v1.begin();i<v1.end();i++){
        for(vector<Point>::iterator j=v2.begin();j<v2.end();j++){
            ans = min(ans,spd((*i).x,(*i).y,(*j).x,(*j).y));
            //cout << (*i).x<< " " << (*i).y << " " << (*j).x << " " << (*j).y << endl;
        }
    }
    return ans;
}

int main(){
    Point begin,end;
    cin >> n;
    cin >> begin.x >> begin.y >> end.x >> end.y;
    for(int i=0;i<n;i++){
        cin >> map[i];
    }
    cout << bfs(begin,end) << endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值