D.C. Connect(搜索)(cf)

给定一个平面网格,Alice想要从一个陆地细胞到另一个陆地细胞,但不能通过水。可能需要创建一条隧道连接两个陆地细胞以使路径可行。题目要求找到创建这条隧道的最小成本。第一例中,最佳方案是从(1,4)到(4,5)建隧道,成本为10。第二例中,需从(1,3)到(3,1)建隧道,成本为8。" 132633172,19453851,C#从零实现奇异值分解(SVD)算法,"['C#编程', '算法实现', '线性代数', '数值计算']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D.C. Connect(搜索)

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’表示水,在水上行走不会耗费金钱,如果要通过水,则需要建隧道,花费费用是(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),求最小花费。

n比较小,可以暴力搜索来搞。

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int mod=9973;
const int N=505050;
const int inf=0x3f3f3f3f;
//priority_queue<int,vector<int>,greater<int> >q;

struct node
{
    int x,y;
} s,t;
vector<node>q[3];
char mp[60][60];
int vis[60][60];
int flag;
int n;
void dfs(int x,int y,int cnt)
{
    if(x>n||x<1||y>n||y<1)
        return;
    if(vis[x][y]==1||mp[x][y]=='1')
        return;
    if(x==t.x&&y==t.y&&cnt==1)
        flag=1;
    vis[x][y]=1;
    node now,to;
    now.x=x;
    now.y=y;
    q[cnt].push_back(now);
    dfs(x+1,y,cnt);
    dfs(x-1,y,cnt);
    dfs(x,y+1,cnt);
    dfs(x,y-1,cnt);
    return;
}
void dfs1(int x,int y,int cnt)
{
    if(x>n||x<1||y>n||y<1)
        return;
    if(vis[x][y]==1||mp[x][y]=='1')
        return;
    vis[x][y]=1;
    node now,to;
    now.x=x;
    now.y=y;
    q[cnt].push_back(now);
    dfs1(x+1,y,cnt);
    dfs1(x-1,y,cnt);
    dfs1(x,y+1,cnt);
    dfs1(x,y-1,cnt);
    return;
}
int main()
{

    scanf("%d",&n);
    flag=0;
    scanf("%d%d",&s.x,&s.y);
    scanf("%d%d",&t.x,&t.y);
    for(int i=1; i<=n; i++)
        scanf("%s",mp[i]+1);
    dfs(s.x,s.y,1);
    int mi=inf;
    if(flag)
        printf("0\n");
    else
    {
        dfs1(t.x,t.y,2);
        for(auto &i:q[1])
        {
            for(auto &j:q[2])
            {
                mi=min(mi,(i.x-j.x)*(i.x-j.x)+(i.y-j.y)*(i.y-j.y));
            }
        }
        printf("%d\n",mi);
    }
    return 0;
}

 

C:\Users\MAXMO>conda create -n secretflow_env python=3.10 Channels: - http://mirrors.aliyun.com/anaconda/pkgs/msys2 - http://mirrors.aliyun.com/anaconda/pkgs/r - http://mirrors.aliyun.com/anaconda/pkgs/main - defaults Platform: win-64 Collecting package metadata (repodata.json): | Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F04CF48710>, 'Connection to repo.anaconda.com timed out. (connect timeout=9.15)')': /pkgs/main/win-64/repodata.json.zst Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F04CF3E330>, 'Connection to repo.anaconda.com timed out. (connect timeout=9.15)')': /pkgs/r/noarch/repodata.json.zst Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F04CF484D0>, 'Connection to repo.anaconda.com timed out. (connect timeout=9.15)')': /pkgs/msys2/win-64/repodata.json.zst Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F04CF3E930>, 'Connection to repo.anaconda.com timed out. (connect timeout=9.15)')': /pkgs/msys2/noarch/repodata.json.zst Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F04CF3EC30>, 'Connection to repo.anaconda.com timed out. (connect timeout=9.15)')': /pkgs/r/win-64/repodata.json.zst Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F04CF48AD0>, 'Connection to repo.anaconda.com timed out. (connect timeout=9.15)')': /pkgs/main/noarch/repodata.json.zst
最新发布
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值