Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4) C - Connect Three

题目:
http://codeforces.com/contest/1087/problem/C

 

代码:
 

//3
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define MAXN 1005
#define INF 0x3f3f3f3f

int ans;
int sx,sy;
int X[4],Y[4];
bool vis[MAXN][MAXN];

int cal(int x,int y)
{
    int i;
    int dis;

    dis=0;
    for(i=1;i<=3;i++)
        dis+=abs(x-X[i])+abs(y-Y[i])-1;
    return dis;
}

void getpath(int id)
{
    int tx,ty;

    tx=sx;
    ty=sy;
    if(tx<X[id])
    {
        while(tx<X[id])
        {
            vis[tx++][ty]=true;
            ans++;
        }
        vis[tx][ty]=true;
    }
    else
    {
        while(tx>X[id])
        {
            vis[tx--][ty]=true;
            ans++;
        }
        vis[tx][ty]=true;
    }
    if(ty<Y[id])
    {
        while(ty<Y[id])
        {
            vis[tx][ty++]=true;
            ans++;
        }
        vis[tx][ty]=true;
    }
    else
    {
        while(ty>Y[id])
        {
            vis[tx][ty--]=true;
            ans++;
        }
        vis[tx][ty]=true;
    }
}

void get_s()
{
    int x,y;
    int temp;
    int dis=INF;

    for(x=0;x<=1000;x++)
    {
        for(y=0;y<=1000;y++)
        {
            temp=cal(x,y);
            if(temp<dis)
            {
                dis=temp;
                sx=x;
                sy=y;
            }
        }
    }
}

int main()
{
    int i;
    int x,y;

    for(i=1;i<=3;i++)
        scanf("%d%d",&X[i],&Y[i]);
    sx=sy=0;
    get_s();
    vis[sx][sy]=true;
    ans=1;
    getpath(1);
    getpath(2);
    getpath(3);
    printf("%d\n",ans);
    for(x=0;x<=1000;x++)
        for(y=0;y<=1000;y++)
            if(vis[x][y])
                printf("%d %d\n",x,y);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值