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.

uploading.4e448015.gif

转存失败重新上传取消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标记找出来,然后暴力所有可能的桥,找到答案

代码:

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';
}

相关推荐

  1. 题解

    2024-06-10 15:58:03       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-10 15:58:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-10 15:58:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-10 15:58:03       82 阅读
  4. Python语言-面向对象

    2024-06-10 15:58:03       91 阅读

热门阅读

  1. HTML做成一个端午节炫酷页面

    2024-06-10 15:58:03       33 阅读
  2. JUC基础_1.JUC概述&&创建线程的方式

    2024-06-10 15:58:03       29 阅读
  3. C++文件系统

    2024-06-10 15:58:03       28 阅读
  4. 第六章 Three.js 光照

    2024-06-10 15:58:03       27 阅读
  5. ArrayList顺序表简单实现

    2024-06-10 15:58:03       22 阅读
  6. LeetCode 380. Insert Delete GetRandom O(1)

    2024-06-10 15:58:03       26 阅读
  7. [leetcode]first-missing-positive 缺失的第一个正数

    2024-06-10 15:58:03       34 阅读
  8. Web前端发展规模:深入探索与未来展望

    2024-06-10 15:58:03       35 阅读
  9. 前端下载图片的几种方式

    2024-06-10 15:58:03       33 阅读