CodeForces 617 D. Polyline(水~)

Description
给出三个整点的坐标,要求用一条由一些水平或者垂直的线段构成的折线将这三个点连起来,问构造这条折线最少需要几条线段
Input
六个整数x1,y1,x2,y2,x3,y3分别表示这三个点的坐标,保证三个点不重复
(-10^9<=xi,yi<=10^9)
Output
输出满足条件的折线最少由几条线段组成
Sample Input
-1 -1
-1 3
4 3
Sample Output
2
Solution
简单题,最多需要三条线段,分析需要1,2,3条线段的情况即可
Code

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int x1,y1,x2,y2,x3,y3,ans;
    while(~scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3))
    {
        if(x1==x2&&x2==x3||y1==y2&&y2==y3)ans=1;
        else if(x2==x1&&(y3<=min(y1,y2)||y3>=max(y1,y2))
            ||x2==x3&&(y1<=min(y3,y2)||y1>=max(y3,y2))
            ||x3==x1&&(y2<=min(y1,y3)||y2>=max(y1,y3))
            ||y2==y1&&(x3<=min(x1,x2)||x3>=max(x1,x2))
            ||y2==y3&&(x1<=min(x3,x2)||x1>=max(x3,x2))
            ||y3==y1&&(x2<=min(x1,x3)||x2>=max(x1,x3)))ans=2;
        else ans=3;
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(CodeForces 617 D. Polyline(水~))