Connect cf题解(dfs,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.

思路:

标记起点和终点可以到的位置,用dfs标记找出来,然后暴力所有可能的桥,找到答案

代码:

cpp 复制代码
int lower_bit(int x){
    return x&(-x);
}
bool check(int x){//判断回文数
    int y=x,t=0;
    while(y){
        t=t*10+y%10;
        y/=10;
    }return x==t;
}
bool cmp1(int a,int b){//从大到小
    return a>b;
}
bool cmpp(pair<char,int>a,pair<char,int>b){
    return a.second<b.second;
}
int pai(int x){//全排列
    if(x==0||x==1)return 1;
    return x*pai(x-1)%mod;
}
char t[100][100];//不能用int ,默认一行一个数
int x[2],y[2];
int vis[2][100][100];
int n;
void dfs(int x,int y,int c){
    if(vis[c][x][y])return ;
    vis[c][x][y]=1;
    if(t[x+1][y]=='0'&&x<n)dfs(x+1,y,c);
    if(t[x][y+1]=='0'&&y<n)dfs(x,y+1,c);
    if(t[x-1][y]=='0'&&x>1)dfs(x-1,y,c);
    if(t[x][y-1]=='0'&&y>1)dfs(x,y-1,c);
}
void solve(){//数据量小,涉及暴力的成分,暴力起点和终点等到达的点的最小距离
   cin>>n;
   for(int i=0;i<2;++i)cin>>x[i]>>y[i];
   for(int i=1;i<=n;++i){
    for(int j=1;j<=n;++j){
        cin>>t[i][j];
    }
   }
   for(int i=0;i<2;++i){
    dfs(x[i],y[i],i);//标记起点和终点可以到的位置
   }
   int ans=1e9+100;
   for(int i=1;i<=n;++i){//暴力,
    for(int j=1;j<=n;++j){
        if(vis[0][i][j]){
            for(int x1=1;x1<=n;++x1){
                for(int y1=1;y1<=n;++y1){
                    if(vis[1][x1][y1]){
                        ans=min(ans,(i-x1)*(i-x1)+(j-y1)*(j-y1));
                    }
                }
            }
        }
    }
   }
   cout<<ans<<'\n';
}
相关推荐
PPPPPaPeR.1 小时前
TopK问题与堆排序
c语言·开发语言·c++·算法
卡戎-caryon2 小时前
【数据结构】06.栈&&队列
c语言·数据结构·算法·链表
山脚ice2 小时前
【CT】LeetCode手撕—704. 二分查找
算法·leetcode
贱贱的剑3 小时前
【算法】选择排序
算法·rust·排序算法
瑜陀3 小时前
2024.06.30 刷题日记
数据结构·算法·leetcode
Star Patrick3 小时前
*算法训练(leetcode)第二十天 | 39. 组合总和、40. 组合总和 II、131. 分割回文串
c++·算法·leetcode
光久li3 小时前
【算法刷题 | 动态规划14】6.28(最大子数组和、判断子序列、不同的子序列)
算法·动态规划
飘然渡沧海3 小时前
gbk,ucs-2转中文
java·开发语言·算法
raykingl3 小时前
154. 寻找旋转排序数组中的最小值 II(困难)
java·python·算法·二分查找
raykingl3 小时前
69. x 的平方根(简单)
java·python·算法·二分查找