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';
}
相关推荐
努力学习编程的伍大侠3 分钟前
基础排序算法
数据结构·c++·算法
XiaoLeisj31 分钟前
【递归,搜索与回溯算法 & 综合练习】深入理解暴搜决策树:递归,搜索与回溯算法综合小专题(二)
数据结构·算法·leetcode·决策树·深度优先·剪枝
Jasmine_llq1 小时前
《 火星人 》
算法·青少年编程·c#
闻缺陷则喜何志丹1 小时前
【C++动态规划 图论】3243. 新增道路查询后的最短距离 I|1567
c++·算法·动态规划·力扣·图论·最短路·路径
Lenyiin1 小时前
01.02、判定是否互为字符重排
算法·leetcode
鸽鸽程序猿2 小时前
【算法】【优选算法】宽搜(BFS)中队列的使用
算法·宽度优先·队列
Jackey_Song_Odd2 小时前
C语言 单向链表反转问题
c语言·数据结构·算法·链表
Watermelo6172 小时前
详解js柯里化原理及用法,探究柯里化在Redux Selector 的场景模拟、构建复杂的数据流管道、优化深度嵌套函数中的精妙应用
开发语言·前端·javascript·算法·数据挖掘·数据分析·ecmascript
乐之者v2 小时前
leetCode43.字符串相乘
java·数据结构·算法