CodeForces C:Connect (BFS+暴力)

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 .

题目的意思是给一张地图,给出起点和终点,地图中0是可以走的,1是水,但是他只能走陆地,可以

任意两个陆地间搭桥,想一下,搭桥的两块陆地,一定是一块起点能走到,一块是终点能走到的,所以

思路来了,从起点和终点分别搜索能走的坐标,起点能走的加入a数组中,终点能走的加入b数组中,数据

不大,遍历a中坐标和b中坐标求任意两点间的距离,取最小即是答案。

#include<iostream>
#include<queue>
#include<stdio.h>
#include<algorithm>
#include<string.h> 
using namespace std;
struct node{
	int x, y;
}a[10010], b[10010];
int vis[55][55];
char map[55][55];
int n, cnt1 = 0, cnt2 = 0, flag = 0;
int dir[4][2] = { 1, 0, -1, 0, 0, -1, 0, 1 };
void bfs(int x, int y){
	queue<node>q;
	q.push({ x, y });
	while (!q.empty())
	{
		int x = q.front().x, y = q.front().y;
		q.pop();
		if (vis[x][y])continue;
		vis[x][y] = 1;
		for (int i = 0; i < 4; i++)
		{
			int xx = x + dir[i][0];
			int yy = y + dir[i][1];
			if (xx >= 1 && xx <= n&&yy >= 1 && yy <= n&&flag == 1 && map[xx][yy] != '1')
				q.push({ xx, yy }), a[cnt1].x = xx, a[cnt1++].y = yy;
			else
				if (xx >= 1 && xx <= n&&yy >= 1 && yy <= n&&flag == 2 && map[xx][yy] != '1')
					q.push({ xx, yy }), b[cnt2].x = xx, b[cnt2++].y = yy;
		}
	}
}
int main(){
	scanf("%d", &n);
	int bx, by, ex, ey;
	scanf("%d%d%d%d", &bx, &by, &ex, &ey);
	for (int i = 1; i <= n; i++)
		scanf("%s", map[i] + 1);
	flag = 1;
	bfs(bx, by);         //搜索起点
	memset(vis, 0, sizeof vis);
	flag = 2;
	bfs(ex, ey);        //搜索终点
	int ans = 0x3f3f3f3f;
	a[cnt1].x = bx, a[cnt1].y = by;    //将起点和终点的坐标分别加入对应数组中
	b[cnt2].x = ex, b[cnt2].y = ey;
	for (int i = 0; i <= cnt1; i++)
		for (int j = 0; j <= cnt2; j++)
		{
			ans = min(ans, (a[i].x - b[j].x)*(a[i].x - b[j].x) + (a[i].y - b[j].y)*(a[i].y - b[j].y)); //取最短搭桥距离;
		}
	cout << ans << endl;
	return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值