• Codeforces Round #542 C. Connect 搜索


    C. Connect
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 (rsrt)2+(csct)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 (1n501≤n≤50 ) — the width of the square grid.

    The second line contains two space-separated integers r1r1 and c1c1 (1r1,c1n1≤r1,c1≤n ) — denoting the cell where Alice resides.

    The third line contains two space-separated integers r2r2 and c2c2 (1r2,c2n1≤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 (1i,jn1≤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 (14)2+(45)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 (13)2+(31)2=8(1−3)2+(3−1)2=8 .

    就是简单的搜索,用dfs求出连通块,然后就遍历起点的所有连通块到终点的连通块,求出最短距离。

    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int maxn = 2550;
    char s[60][60];
    int n, sx, sy, gx, gy,ans=inf;
    int dx[4] = { 0,1,-1,0 };
    int dy[4] = { 1,0,0,-1 };
    bool vis[60][60];
    struct node
    {
        int x, y;
        node(int x = 0, int y = 0) :x(x), y(y) {}
    };
    int cnt = 0;
    void dfs(int x, int y, node *d)
    {
        for (int i = 0; i < 4; i++)
        {
            int tx = x + dx[i];
            int ty = y + dy[i];
    
            if (vis[tx][ty]) continue;
            if (s[tx][ty] == '1') continue;
            if (tx<1 || ty<1 || tx>n || ty>n) continue;
    
            vis[tx][ty] = true;
            d[cnt++] = node(tx, ty);
            dfs(tx, ty, d);
        }
    }
    int min_(int a,int b)
    {
        return a < b ? a : b;
    }
    
    int main()
    {
        cin >> n >> sx >> sy >> gx >> gy;
        for (int i = 1; i <= n; i++) cin >> s[i] + 1;
        node a[maxn], b[maxn];
        a[0] = node(sx, sy); b[0] = node(gx, gy);
        vis[sx][sy] = true;cnt = 1;
        dfs(sx, sy, a);
        if(vis[gx][gy])
        {
            printf("0
    ");
            return 0;
        }
        int tot = cnt;cnt = 1;
        vis[gx][gy] = 1;
        dfs(gx, gy, b);
        //printf("%d %d
    ", cnt, tot);
        for(int i=0;i<tot;i++)
        {
            for(int j=0;j<cnt;j++)
            {
                //printf("%d %d %d %d
    ", a[i].x, a[i].y, b[j].x, b[j].y);
                int exa = (a[i].x - b[j].x)*(a[i].x - b[j].x) + (a[i].y - b[j].y)*(a[i].y - b[j].y);
                //printf("%d
    ", exa);
                ans = min_(ans, exa);
            }
        }
        printf("%d
    ", ans);
        return 0;
    }
  • 相关阅读:
    ASP.NET MVC的过滤器
    EasyUi 合并单元格占列显示
    Ajax异步请求阻塞情况的解决办法
    AutoFac使用总结
    CodeFirst迁移注意点
    DataTable与实体类互相转换
    mysql中注释的添加修改
    MySql修改数据表的基本操作(DDL操作)
    MySql中的约束
    如何使用单例模式返回一个安全的Connection
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10539014.html
Copyright © 2020-2023  润新知