1011. Cut Rectangles (35)

1.题面

点击打开链接

2.题意

给出两个多边形,问这两个多边形能否由一条直线切割长方形得到。

3.思路

有一个测试点死活过不去,还在纠结,代码先晒在这里。

4.代码

/*****************************************************************
    > File Name: cpp_acm.cpp
    > Author: Uncle_Sugar
    > Mail: [email protected]
    > Created Time: Tue 21 Feb 2017 03:25:16 CST
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

# define rep(i,a,b) for (i=a;i<=b;i++)
# define rrep(i,a,b) for (i=b;i>=a;i--)
# define mset(aim, val) memset(aim, val, sizeof(aim))

struct QuickIO{
    QuickIO(){const int SZ = 1<<20;
        setvbuf(stdin ,new char[SZ],_IOFBF,SZ);
        setvbuf(stdout,new char[SZ],_IOFBF,SZ);
    }                //*From programcaicai*//
}QIO;

template<class T>void PrintArray(T* first,T* last,char delim=' '){
    for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim);
}

/*
1.see the size of the input data before you select your algorithm 
2.cin&cout is not recommended in ACM/ICPC
3.pay attention to the size you defined, for instance the size of edge is double the size of vertex
*/

const int debug = 1;
// const int size  = 10 + ; 
const int INF = INT_MAX>>1;
const double eps = 1e-7;
typedef long long ll;

struct Point{
    double x, y;
    Point(){}
    Point(double _x, double _y):x(_x), y(_y){}
};
typedef Point Vector;
Vector operator - (const Point& p1, const Point& p2){
    return Vector(p1.x - p2.x, p1.y - p2.y);
}

double DotProduct(const Vector& v1, const Vector& v2){
    return v1.x*v2.x + v1.y*v2.y;
} 
double Length(const Vector v){
    return sqrt(DotProduct(v, v));
}

double CrossProduct(const Vector& v1, const Vector& v2){
    return v1.x*v2.y - v1.y*v2.x;
} 



vector<Point> vct[2];
vector<Vector> special[2];
vector<double> edgelength[2];

int main(){
    // std::ios::sync_with_stdio(false);cin.tie(0);
    int T; scanf("%d", &T);
    while (T--){
        for (int i = 0; i < 2; i++) {
            special[i].clear();
            vct[i].clear();
            edgelength[i].clear();
        }
        int ans = 1;
        for (int ii = 0; ii < 2; ii++){
            int k;scanf("%d", &k);
            while (k--){
                Point tp;scanf("%lf%lf", &tp.x, &tp.y);
                vct[ii].push_back(tp);
            }
        }
        for (int ii = 0; ii < 2; ii++){
            int t = -1;
            for (int i = 0; i < vct[ii].size()-1; i++){
                int j = (i+1)%vct[ii].size();
                int k = (i+2)%vct[ii].size();
                // cout << CrossProduct(vct[ii][j] - vct[ii][i], vct[ii][k] - vct[ii][i]) << " ";
                if (t == -1) t = (CrossProduct(vct[ii][j] - vct[ii][i], vct[ii][k] - vct[ii][i]) < 0);
                else if (t != (CrossProduct(vct[ii][j] - vct[ii][i], vct[ii][k] - vct[ii][i]) < 0)){
                    ans = 0;
                    break;
                }
            }
            // cout << endl;
        }
        
        if (!ans){
            printf("NO\n");
            continue;
        }

        for (int ii = 0; ii < 2; ii++){
            for (int i = 0; i < vct[ii].size(); i++){
                int j = (i+1)%vct[ii].size();
                Vector v = vct[ii][j] - vct[ii][i];
                edgelength[ii].push_back(Length(v));
                if (abs(v.x) > eps && abs(v.y) > eps){
                    special[ii].push_back(v);
                }
            }
        }
        // cout << special[0].size() << " " << special[1].size() << endl;
        if (
            !(special[0].size() == special[1].size() && (special[0].size() == 0 || special[1].size() == 1))
           ){
            printf("NO\n");
            continue;
        }
        if (special[0].size() == 0){
            ans = 0;
            for (int i = 0; i < 4; i++){
                for (int j = 0; j < 4; j++){
                    if (abs(edgelength[0][i] - edgelength[1][j]) < eps){
                        ans = 1;
                        break;
                    }
                }
                if (ans == 1)
                    break;
            }
            if (ans)
                printf("YES\n");
            else 
                printf("NO\n");
        }else if (special[0].size() == 1){
            for (int i = 0; i < 2; i++){
                special[i][0].x = abs(special[i][0].x);
                special[i][0].y = abs(special[i][0].y);
                if (special[i][0].x < special[i][0].y)
                    swap(special[i][0].x, special[i][0].y);
            }
            if (Length(special[0][0] - special[1][0]) < eps)
                printf("YES\n");
            else 
                printf("NO\n");
        }
    }
    return 0;
}










猜你喜欢

转载自blog.csdn.net/sinat_29278271/article/details/56241408
cut
35
今日推荐