Coedforce993A(数学思维+暴力)

You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coordinate axes. Find whether the two squares intersect.

The interior of the square is considered to be part of the square, i.e. if one square is completely inside another, they intersect. If the two squares only share one common point, they are also considered to intersect.

Input

The input data consists of two lines, one for each square, both containing 4 pairs of integers. Each pair represents coordinates of one vertex of the square. Coordinates within each line are either in clockwise or counterclockwise order.

The first line contains the coordinates of the square with sides parallel to the coordinate axes, the second line contains the coordinates of the square at 45 degrees.

All the values are integer and between 100−100 and 100100.

Output

Print "Yes" if squares intersect, otherwise print "No".

You can print each letter in any case (upper or lower).

Examples
Input
0 0 6 0 6 6 0 6
1 3 3 5 5 3 3 1
Output
YES
Input
0 0 6 0 6 6 0 6
7 3 9 5 11 3 9 1
Output
NO
Input
6 0 6 6 0 6 0 0
7 4 4 7 7 10 10 7
Output
YES
Note

In the first example the second square lies entirely within the first square, so they do intersect.

In the second sample squares do not have any points in common.

Here are images corresponding to the samples:

  • 题意:平行于x轴的正方形和与x轴成45度倾斜的正方形,判断两个正方形是否相交。
  • 解题思路:与x轴呈45度倾斜的正方形,由于其四个顶点都为整数点,并且其边的斜率已经确定。若给定的两个正方形相交,必定至少存在一个整数点分别在两个正方形区域内。
  • AC Code
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    
    struct Data       //结构体保存正方形区域内的点的坐标 
    {
    	int x, y;
    };
    Data st1[40000+5];   //注意结构体数组的大小,200*200+5,否则会RE 
    Data st2[40000+5];
    int main()
    {
    	int x1, x2, x3, x4;
    	int y1, y2, y3, y4;
    	int x5, x6, x7, x8;
    	int y5, y6, y7, y8;
    	scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
    	scanf("%d %d %d %d %d %d %d %d",&x5,&y5,&x6,&y6,&x7,&y7,&x8,&y8);
    	int minx = x1;               //求出第一个正方形的边界x,y.用于枚举出整数点 
    	if(minx > x2) minx = x2;
    	if(minx > x3) minx = x3;
    	if(minx > x4) minx = x4;
    	
    	int maxx = x1;
    	if(maxx < x2) maxx = x2;
    	if(maxx < x3) maxx = x3;
    	if(maxx < x4) maxx = x4;
      	
      	int miny = y1;
      	if(miny > y2) miny = y2;
      	if(miny > y3) miny = y3;
      	if(miny > y4) miny = y4;
      	
      	int maxy = y1;
      	if(maxy < y2) maxy = y2;
      	if(maxy < y3) maxy = y3;
      	if(maxy < y4) maxy = y4;
     	
     	int k = 0;                      //第一个正方形枚举点的坐标 
     	for(int i=minx; i<=maxx; i++)
     	{
     		for(int j=miny; j<=maxy; j++)
     		{
     			++k;
     			st1[k].x = i;
     			st1[k].y = j;
     		}
     	}
     	int minx2 = x5;          //求出第二个正方形的边界x,y.用于枚举出整数点 
     	if(minx2 > x6) minx2 = x6;
    	if(minx2 > x7) minx2 = x7;
    	if(minx2 > x8) minx2 = x8;
    	
    	int maxx2 = x5;
    	if(maxx2 < x6) maxx2 = x6;
    	if(maxx2 < x7) maxx2 = x7;
    	if(maxx2 < x8) maxx2 = x8;
    	
    	int miny2 = y5;
    	if(miny2 > y6) miny2 = y6;
    	if(miny2 > y7) miny2 = y7;
    	if(miny2 > y8) miny2 = y8;
    	
    	int maxy2 = y5;
    	if(maxy2 < y6) maxy2 = y6;
    	if(maxy2 < y7) maxy2 = y7;
    	if(maxy2 < y8) maxy2 = y8;
    	
    	int x0 = (minx2 + maxx2) / 2;
    	int y0 = (miny2 + maxy2) / 2;
    	
    	int t = 0;
    	for(int i=minx2; i<=maxx2; i++)	//第一个正方形枚举点的坐标 
    	{
    		for(int j=miny2; j<=maxy2; j++)
    		{
    			int a = j - i - y0 + minx2;    //利用线性规划将区域外的点略去 
    			int b = j + i - maxx2 - y0;
    			int c = j - i - y0 + maxx2;
    			int d = j + i -y0 - minx2;
    			//printf("%d %d : %d %d %d %d\n",i,j,a,b,c,d);
    			if(a<=0 && b<=0 && c>=0 && d>=0) 
    			{
    				t++;
    				st2[t].x = i;
    				st2[t].y = j;
    			} 
    		}
    	}
    //	printf("\n");
    //	for(int i=1; i<=k; i++)
    //	{
    //		printf("%d %d\n",st1[i].x,st1[i].y);
    //	}
    //	printf("\n");
    //	printf("\n");
    //	for(int i=1; i<=t; i++)
    //	{
    //		printf("%d %d\n",st2[i].x,st2[i].y);
    //	}
    	int w = 0;
    	for(int i=1; i<=k; i++)
    	{
    		for(int j=1; j<=t; j++) 
    		{
    			if(st1[i].x == st2[j].x && st1[i].y == st2[j].y)	w = 1;
    			if(w==1) break;
    		}
    		if(w==1) break;
    	}
    	if(w==1) printf("YES\n");
    	else  printf("NO\n");
    	return 0;
    }



猜你喜欢

转载自blog.csdn.net/Alibaba_lhl/article/details/81039995