前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C. Connect 【 BFS + 暴力 】

C. Connect 【 BFS + 暴力 】

作者头像
Lokinli
发布2023-03-09 19:36:09
2420
发布2023-03-09 19:36:09
举报
文章被收录于专栏:以终为始以终为始

Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)

题意:给你一个 n * n 的图,给你起点和终点,只要是 0 的位置就可以随便移动,可以上下左右移动,问从起点到终点的最小距离的平方,这里的距离是欧几里得距离,可以通过走 0 来减小距离。

&: BFS 两个点,暴力可以走的点的数组求最小距离。

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
struct node {
    int x;
    int y;
}l,w,s[20000],e[20000];
int n,sx,sy,ex,ey;
bool vis[200][200];
char gra[200][200];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
void bfs(int x, int y, struct node a[], int &p)
{
    memset(vis,false,sizeof(vis));
    queue<node> q;
    l.x = x-1;l.y = y-1;
    q.push(l);
    vis[x-1][y-1] = true;
    a[p].x = x-1;
    a[p ++].y = y-1;
    while(!q.empty())
    {
        w = q.front();
        q.pop();
        for(int i = 0;i < 4; i ++)
        {
            int tx = w.x + dx[i];
            int ty = w.y + dy[i];
            if(tx >= 0 && tx < n && ty >= 0 && ty < n && gra[tx][ty] == '0' && vis[tx][ty] == false)
            {
                l.x = tx;
                l.y = ty;
                a[p].x = tx;
                a[p ++].y = ty;
                q.push(l);
                vis[tx][ty] = true;
            }
        }
    }
//    return ;
}
int main()
{
    scanf("%d", &n);
    scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
    for(int i = 0; i < n; i ++)
    {
        getchar();
        scanf("%s", gra[i]);
    }
    int p1 = 0;
    int p2 = 0;
    bfs(sx,sy,s,p1);
    bfs(ex,ey,e,p2);
//    printf("%d %d\n",p1,p2);
//    for(int i = 0; i < p1; i ++) cout << s[i].x  << " " << s[i].y << endl;
//    cout << "dddddddddd" << endl;
//    for(int i = 0; i < p2; i ++) cout << e[i].x << " " << e[i].y << endl;
    int sum = 0x3f3f3f3f;
    for(int i = 0; i < p1; i ++)
    {
        for(int j = 0; j < p2; j ++){
 
            sum = min((s[i].x - e[j].x) * (s[i].x - e[j].x) + (s[i].y - e[j].y) * (s[i].y - e[j].y),sum);
        }
    }
    printf("%d\n",sum);
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档