C. Two Squares
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
Copy
0 0 6 0 6 6 0 6 1 3 3 5 5 3 3 1
output
Copy
YES
input
Copy
0 0 6 0 6 6 0 6 7 3 9 5 11 3 9 1
output
Copy
NO
input
Copy
6 0 6 6 0 6 0 0 7 4 4 7 7 10 10 7
output
Copy
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:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RU8ArNc8-1611925768983)(https://espresso.codeforces.com/7cee584e1c522d31a1a3be13929a736a2c5a3792.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gLykqUXJ-1611925768985)(https://espresso.codeforces.com/53ffe663b0a4477e72b956c2030e803f031f9fd2.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oGXwoyVI-1611925768986)(https://espresso.codeforces.com/65f51282cf774e5200930d3189786453a90708fb.png)]
这个题其实暴力就可以
Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctype.h>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <sstream>
#define ll unsigned long long
#define re return
using namespace std;
typedef pair<int,int>p;
map<p, int> m; //地图,用map就能很好的解决负数坐标的问题
struct nope
{
int x, y;
}ax[6],bx[6], ay[6],by[6]; //好像写的有点毒瘤
bool cmp(nope a, nope b){
return a.x < b.x;
// return a.y < b.y;
}
bool cmp1(nope a, nope b){
return a.y < b.y;
}
int main(){
// freopen("D.out","w",stdout);
for (int i = 1; i <= 4; i++){
cin >> ax[i].x >> ax[i].y;
ay[i].x = ax[i].x;
ay[i].y = ax[i].y;
}
for (int i = 1; i <= 4; i++){
cin >> bx[i].x >> bx[i].y;
by[i].x = bx[i].x;
by[i].y = bx[i].y;
}
// int num1 = abs(a[1].x - a[2].x) + abs(a[1].y - a[2].y);
// int num2 = sqrt(pow(abs(b[1].x - b[2].x), 2) + pow(abs(b[1].y - b[2].y), 2)) * sqrt(2); //对角线长,好像没用到
// cout << num1 << " " << num2 << endl;
sort(ax + 1, ax + 1 + 4, cmp); //因为一个是平行放的,一个是斜45度。平着的这个x中坐标中一定有两对是相同的,同理y也有两对是相同的
sort(ay + 1, ay + 1 + 4, cmp1);
for (int i = ax[1].x; i <= ax[3].x; i++){//第一个和第二个相同,第三个开始大
for (int j = ay[1].y; j <= ay[3].y; j++){
m[p(i, j)] = 1;
}
}
sort(bx + 1, bx + 1 + 4, cmp);
sort(by + 1, by + 1 + 4, cmp1);
// for (int i = 1; i <= 4; i++){
// cout << by[i].x << " " << by[i].y << endl;
// }
// cout << endl;
// for (int i = 1; i <= 4; i++){
// cout << bx[i].x << " " << bx[i].y << endl;
// }
int cnt = 0;
int t = 0;
for (int i = bx[1].x; i <= bx[2].x; i++){ //枚举左半边
for (int j = by[2].y - cnt; j <= by[2].y + cnt; j++){
if(m[p(i, j)]){
t = 1;
break;
}
}
cnt ++;
}
if(t == 1){
cout << "YES" << endl;
}
else{
cnt = 0;
for (int i = bx[4].x; i >= bx[3].x; i--){ //枚举右半边
for (int j = by[2].y - cnt; j <= by[2].y + cnt; j++){
if(m[p(i, j)]){
t = 1;
break;
}
}
cnt++;
}
if(t == 1){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
// 1 3
// 3 5
// 3 1
// 5 3
// for (int i = 1; i <= 100; i++){
// for (int j = 100; j >= -10; j--){
// cout << m[p(i,j)] << " ";
// }
// cout << endl;
// }
re 0;
}
// 3 1
// 5 3
// for (int i = 1; i <= 100; i++){
// for (int j = 100; j >= -10; j--){
// cout << m[p(i,j)] << " ";
// }
// cout << endl;
// }
re 0;
}