题目:
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;
}