Cut Rectangles

When a rectangle is cut by a straight line, we can easily obtain two polygons as the result. But the reversed problem is harder: given two polygons, your task is to check whether or not they could be obtained by cutting a rectangle.

To give you more trouble, the input polygons are possibly moved, rotated (90 degrees, 180 degrees, or 270 degrees counter-clockwise), or even flipped (mirrored).

It is assumed that the original rectangle's edges are parallel to the axis.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤), and then N pairs of polygons are given. Each polygon is described in the format:

x1​​ y1​​ ⋯ xk​​ yk​​

where k (2) is the number of vertices on the polygon, and (xi​​, yi​​) (0) are the coordinates of the vertices, given in either clockwise or counter-clockwise order.

Note: there is no redundant vertex. That is, it is guaranteed that all the vertices are distinct for each polygon, and that no three consecutive vertices are on the same line.

Output Specification:

For each pair of polygons, print in a line either YES or NO as the answer.

Sample Input:

8
3 0 0 1 0 1 1
3 0 0 1 1 0 1
3 0 0 1 0 1 1
3 0 0 1 1 0 2
4 0 4 1 4 1 0 0 0
4 4 0 4 1 0 1 0 0
3 0 0 1 1 0 1
4 2 3 1 4 1 7 2 7
5 10 10 10 12 12 12 14 11 14 10
3 28 35 29 35 29 37
3 7 9 8 11 8 9
5 87 26 92 26 92 23 90 22 87 22
5 0 0 2 0 1 1 1 2 0 2
4 0 0 1 1 2 1 2 0
4 0 0 0 1 1 1 2 0
4 0 0 0 1 1 1 2 0
 

Sample Output:

YES
NO
YES
YES
YES
YES
NO
YES
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 int main()
  4 {
  5 //  ios::sync_with_stdio(false);
  6  //   freopen("data.txt","r",stdin);
  7     int n,k,x;
  8     int c1,c2;
  9     scanf("%d",&n);
 10     for(;n--;)
 11     {
 12         int p411=-1,p412=-1,p421=-1,p422=-1;
 13         vector<pair<int,int> > v1,v2; 
 14         bool flag=false;
 15         pair<int,int> p1(0,0);
 16         pair<int,int> p2(0,0);
 17         scanf("%d",&k);
 18         for(int i=0;i<k;i++)
 19         {
 20             scanf("%d %d",&c1,&c2);
 21             if((!v1.empty())&&(c1-v1.back().first)&&(c2-v1.back().second))
 22             {
 23                 if(!p1.first||!p1.second)
 24                 {
 25                     p1.first=abs(c1-v1.back().first);
 26                     p1.second=abs(c2-v1.back().second);
 27                     if(k>3)
 28                     {
 29                         p411=i-1;
 30                         p412=i;
 31                     }
 32                 }
 33                 else
 34                 {
 35                     p1.first=-1;
 36                     p1.second=-1;                    
 37                 }
 38             }
 39             v1.push_back(pair<int,int>(c1,c2));
 40         }
 41         if(!p1.first||!p1.second)
 42         {
 43             p1.first=abs(v1.front().first-v1.back().first);
 44             p1.second=abs(v1.front().second-v1.back().second);
 45             if(k>3)
 46             {
 47                 p411=k-1;
 48                 p412=0;
 49             }
 50         }
 51         else if((v1.front().first-c1)&&(v1.front().second-c2))
 52         {
 53             p1.first=-1;
 54             p1.second=-1;
 55         }
 56         scanf("%d",&k);
 57         for(int i=0;i<k;i++)
 58         {
 59             scanf("%d %d",&c1,&c2);
 60             if((!v2.empty())&&(c1-v2.back().first)&&(c2-v2.back().second))
 61             {        
 62                 if(!p2.first||!p2.second)
 63                 {
 64                     p2.first=abs(c1-v2.back().first);
 65                     p2.second=abs(c2-v2.back().second);
 66                     if(k>3)
 67                     {
 68                         p421=i-1;
 69                         p422=i;
 70                     }
 71                 }
 72                 else
 73                 {
 74                     p2.first=-2;
 75                     p2.second=-2;
 76                 }
 77             }
 78             v2.push_back(pair<int,int>(c1,c2));
 79         }
 80         if(!p2.first||!p2.second)
 81         {
 82             p2.first=abs(v2.front().first-v2.back().first);
 83             p2.second=abs(v2.front().second-v2.back().second);
 84             if(k>3)
 85             {
 86                 p421=k-1;
 87                 p422=0;
 88             }            
 89         }
 90         else if((v2.front().first-c1)&&(v2.front().second-c2))
 91         {
 92             p2.first=-2;
 93             p2.second=-2;
 94         }
 95         if(p1.first>p1.second)
 96         swap(p1.first,p1.second);
 97         if(p2.first>p2.second)
 98         swap(p2.first,p2.second);
 99         if(p1==p2)
100         {
101             if(v1.size()==3)
102             flag=true;
103             else if(v1.size()==4)
104             {
105                 flag=true;
106                 if(v2.size()==4)
107                 {
108                     if(!p1.first)
109                     flag=true;
110                     else if(p1.first!=p1.second)
111                     {
112                         int m1=max(abs(v1[(p411+2)%4].first-v1[(p411+3)%4].first),abs(v1[(p411+2)%4].second-v1[(p411+3)%4].second));
113                         int m2=max(abs(v2[(p421+2)%4].first-v2[(p421+3)%4].first),abs(v2[(p421+2)%4].second-v2[(p421+3)%4].second));
114                         if(m1!=m2)
115                         flag=false;
116                     }
117                 }
118                 else if(v2.size()>4)
119                 flag=false;
120             }
121             else if(v1.size()==5)
122             {
123                 if(v2.size()==3)
124                 flag=true;
125             }
126         }
127         if(flag)
128         printf("YES\n");
129         else
130         printf("NO\n");
131     }
132       return 0;
133 }

 

posted @ 2020-02-09 00:19  一斜星辰酱  阅读(153)  评论(0编辑  收藏  举报