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';
}
相关推荐
一直学习永不止步2 分钟前
LeetCode题练习与总结:赎金信--383
java·数据结构·算法·leetcode·字符串·哈希表·计数
小刘|34 分钟前
《Java 实现希尔排序:原理剖析与代码详解》
java·算法·排序算法
jjyangyou38 分钟前
物联网核心安全系列——物联网安全需求
物联网·算法·安全·嵌入式·产品经理·硬件·产品设计
van叶~1 小时前
算法妙妙屋-------1.递归的深邃回响:二叉树的奇妙剪枝
c++·算法
简简单单做算法1 小时前
基于Retinex算法的图像去雾matlab仿真
算法·matlab·图像去雾·retinex
云卓SKYDROID1 小时前
除草机器人算法以及技术详解!
算法·机器人·科普·高科技·云卓科技·算法技术
半盏茶香2 小时前
【C语言】分支和循环详解(下)猜数字游戏
c语言·开发语言·c++·算法·游戏
徐子童2 小时前
双指针算法习题解答
算法
想要打 Acm 的小周同学呀2 小时前
LRU缓存算法
java·算法·缓存
劲夫学编程3 小时前
leetcode:杨辉三角
算法·leetcode·职场和发展