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';
}
相关推荐
alphaTao18 分钟前
LeetCode 每日一题 2024/11/18-2024/11/24
算法·leetcode
kitesxian27 分钟前
Leetcode448. 找到所有数组中消失的数字(HOT100)+Leetcode139. 单词拆分(HOT100)
数据结构·算法·leetcode
VertexGeek1 小时前
Rust学习(八):异常处理和宏编程:
学习·算法·rust
石小石Orz1 小时前
Three.js + AI:AI 算法生成 3D 萤火虫飞舞效果~
javascript·人工智能·算法
jiao_mrswang2 小时前
leetcode-18-四数之和
算法·leetcode·职场和发展
qystca2 小时前
洛谷 B3637 最长上升子序列 C语言 记忆化搜索->‘正序‘dp
c语言·开发语言·算法
薯条不要番茄酱2 小时前
数据结构-8.Java. 七大排序算法(中篇)
java·开发语言·数据结构·后端·算法·排序算法·intellij-idea
今天吃饺子2 小时前
2024年SCI一区最新改进优化算法——四参数自适应生长优化器,MATLAB代码免费获取...
开发语言·算法·matlab
是阿建吖!2 小时前
【优选算法】二分查找
c++·算法
王燕龙(大卫)2 小时前
leetcode 数组中第k个最大元素
算法·leetcode