zoukankan      html  css  js  c++  java
  • connect ~穿过山水只为你

    链接

    http://codeforces.com/problemset/problem/1130/C

    题目内容: 

    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
    5
    1 1
    5 5
    00001
    11111
    00111
    00110
    00110
    
    output
    10
    
    input
     
    3
    1 3
    3 1
    010
    101
    010
    
    output
    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.

    题目大意:  这道题还是比较好理解的,只是让你求一下从点1走到点2需要修多长的路,因为你不能走水路,必须在水路上修桥,求的就是桥的最小距离

    大体思路:这道题考察的是bfs,因为我们只用统计修桥的最短距离,因此我们可以统计两片陆地的情况,即通过bfs算法把从起点可以走到陆地标记成一个值,从

    终点走的路程标记为另一个值,这样我们只需要求出不同标记值之间的最短距离(地图的标记都是陆地)

    代码代码!!!!

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<queue>
    using namespace std;
    #define maxn 55
    #define inf 0x3f3f3f3f
    char map[maxn][maxn];
    int vis[maxn][maxn],n;
    int dir[4][2]={1,0,-1,0,0,1,0,-1};
    struct node
    {
        int x,y;
    };
    void dfs(int x,int y,int flag)
    {
        queue<node>q;
        node now;
        now.x=x;
        now.y=y;
        q.push(now);
        vis[x][y]=flag;
        while(!q.empty())    
        {
            node sx,ex;
            sx=q.front();
            q.pop();
            for(int i=0;i<4;i++)
            {
                ex.x=sx.x+dir[i][0];
                ex.y=sx.y+dir[i][1];
                if(ex.x>0&&ex.y>0&&ex.x<=n&&ex.y<=n&&vis[ex.x][ex.y]==0&&map[ex.x][ex.y]=='0')
                {
                    q.push(ex);
                    vis[ex.x][ex.y]=flag;
                }
            }
        }
    }
    int main()
    {
        int sx,sy,ex,ey;
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
        for(int i=1;i<=n;i++)   scanf("%s",map[i]+1);
        dfs(sx,sy,1);
        if(vis[ex][ey]==1)
        {
            printf("0
    ");
            return 0;
        }
        dfs(ex,ey,-1);
        int ans=inf;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(vis[i][j]==1&&map[i][j]=='0')
                {
                    for(int a=1;a<=n;a++)
                    {
                        for(int b=1;b<=n;b++)
                        {
                            if(vis[a][b]==-1&&map[a][b]=='0')
                             ans=min(ans,(i-a)*(i-a)+(j-b)*(j-b));
                        }
                    }
                }
            }
        }
          printf("%d
    ",ans);
     
        return 0;
    } 
     好的,就这样
  • 相关阅读:
    不负时光,不负自己
    理解无偏估计(unbiased estimation)
    Latex Error:‘acmart.cls’ not found 解决方案:
    Dark theme for Texstudio
    马尔可夫毯(Markov Blanket)
    时间复杂度和空间复杂度的简单讲解
    应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测
    ElasticSearch集群状态查看命令大全
    ElasticSearch API 之 UPDATE
    ElasticSearch API 之 DELETE
  • 原文地址:https://www.cnblogs.com/tombraider-shadow/p/10486115.html
Copyright © 2011-2022 走看看