【暴力BFS】Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) C. Connect

C. Connect

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

50*50 的图  0代表陆地 1代表水

给你起点终点,可以上下左右走陆地,问你起点这块和终点那块各选一个可以走的方位算距离,输出最小的

直接暴力bfs起点和终点,然后把能走的互相配

#include <bits/stdc++.h>
using namespace std;
char a[55][55];
int walk[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
struct node
{
    int x,y;
};
queue <node> q,q1;
int vis[55][55],vis1[55][55],n;

void bfs(int s,int t,int judge)
{
    node qi;
    qi.x=s,qi.y=t;
    q.push(qi);
    if(judge) vis[s][t]=1;
    else vis1[s][t]=1;
    while(!q.empty())
    {
        node p=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=p.x+walk[i][0],yy=p.y+walk[i][1];
            if(xx<=0||xx>n||yy<=0||yy>n||a[xx][yy]=='1') continue;
            if(judge&&vis[xx][yy]) continue;
            else if(!judge&&vis1[xx][yy]) continue;

            if(judge) vis[xx][yy]=1;
            else vis1[xx][yy]=1;
            node o;
            o.x=xx,o.y=yy;
            q.push(o);

        }
    }
}


int main()
{
    cin>>n;
    int sx,sy,tx,ty;
    cin>>sx>>sy>>tx>>ty;
    for(int i=1; i<=n; i++)
    {
        scanf("%s",a[i]+1);
    }
    bfs(sx,sy,1);
    bfs(tx,ty,0);
//
//    for(int i=1; i<=n; i++)
//    {
//        for(int j=1; j<=n; j++)
//        {
//            if(vis[i][j]) cout<<i<<" "<<j<<endl;
//        }
//    }
//
//    cout<<endl;
//
//    for(int i=1; i<=n; i++)
//    {
//        for(int j=1; j<=n; j++)
//        {
//            if(vis1[i][j]) cout<<i<<" "<<j<<endl;
//        }
//    }


    int minn=1e8;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            for(int i1=1; i1<=n; i1++)
            {
                for(int j1=1; j1<=n; j1++)
                {
                    if(vis[i][j]&&vis1[i1][j1])
                    {
                        minn=min(minn,(i-i1)*(i-i1)+(j-j1)*(j-j1));
                    }
                }
            }
        }
    }
    printf("%d\n",minn);

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值