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:

题目意思:

给你两个矩形,第一行是一个正面表示的矩形,第二个是一个旋转四十五度角的矩形,问这两个矩形是否相交

因为题目数据范围很小,所以很容易想到的是暴力枚举每个矩形中的每个点,若有点既在第一个矩形又在第二个矩形内则正面两个矩形相交

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl;
using namespace std;
const int maxn = 1e3 + ;
typedef long long ll;
struct point {
ll x, y;
};
bool cmp( point p, point q ) {
if( p.x == q.x ) {
return p.y < q.y;
}
return p.x < q.x;
}
point a[], b[];
ll vis[maxn][maxn];
bool ok() {
for( ll i = a[].x; i <= a[].x; i ++ ) {
for( ll j = a[].y; j <= a[].y; j ++ ) {
vis[i+][j+] = ;
}
}
  //注意枚举第二个矩形的点的时候,循环条件要写明白,不要把矩形外的点枚举进来
for( ll i = b[].x; i <= b[].x; i ++ ) {
for( ll j = ; j <= i - b[].x; j ++ ) {
if( vis[i+][b[].y+j+] || vis[i+][b[].y-j+] ) {
return true;
}
}
}
for( ll i = b[].x; i <= b[].x; i ++ ) {
for( ll j = ; j <= b[].y-b[].y-(i-b[].x); j ++ ) {
if( vis[i+][b[].y+j+] || vis[i+][b[].y-j+] ) {
return true;
}
}
}
return false;
}
int main(){
std::ios::sync_with_stdio(false);
while( cin >> a[].x >> a[].y >> a[].x >> a[].y >> a[].x >> a[].y >> a[].x >> a[].y >>
b[].x >> b[].y >> b[].x >> b[].y >> b[].x >> b[].y >> b[].x >> b[].y ) {
memset( vis, , sizeof(vis) );
sort( a + , a + , cmp );
sort( b + , b + , cmp );
if( ok() ) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return ;
}

第二种方法是根据两个矩形的相交性质写判断条件,开始自己写的时候想错条件了wa了几发

判断条件是这样的,首先是内含,那只要第二个矩形的点的坐标在第一个矩形的最小和最大之间就满足条件

第二种是两个矩形相交,判断点的坐标之和、坐标之差,只要第一个矩形的坐标之和、坐标之差在第二个矩形的最大最小坐标之和、最大最小坐标之差之间

第三种是两条边刚好有交点,只要第一个矩形的坐标之和、坐标之差在第二个矩形的最大最小坐标之和、最大最小坐标之差的两倍之间

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl;
using namespace std;
const int maxn = 1e3 + ;
typedef long long ll;
struct point {
ll x, y;
};
point a[], b[];
ll vis[maxn][maxn];
bool ok() { ll minax = , maxax = -, minay = , maxay = -;
for( ll i = ; i <= ; i ++ ) {
minax = min( minax, a[i].x );
maxax = max( maxax, a[i].x );
minay = min( minay, a[i].y );
maxay = max( maxay, a[i].y );
}
ll xpymin = , xpymax = -, xmymin = , xmymax = -;
for( ll i = ; i <= ; i ++ ) {
xpymin = min( xpymin, b[i].x + b[i].y );
xpymax = max( xpymax, b[i].x + b[i].y );
xmymin = min( xmymin, b[i].x - b[i].y );
xmymax = max( xmymax, b[i].x - b[i].y );
}
for( ll i = ; i <= ; i ++ ) {
ll x = b[i].x, y = b[i].y;
if( x >= minax && x <= maxax && y >= minay && y <= maxay ) {
return true;
}
ll xpy = a[i].x + a[i].y, xmy = a[i].x - a[i].y;
if( xpy >= xpymin && xpy <= xpymax && xmy >= xmymin && xmy <= xmymax ) {
return true;
}
}
ll x = minax + maxax,y = minay + maxay;
ll xpy = x + y, xmy = x - y;
if( xpy >= *xpymin && xpy <= *xpymax && xmy >= *xmymin && xmy <= *xmymax ) {
return true;
}
return false;
}
int main(){
std::ios::sync_with_stdio(false);
while( cin >> a[].x >> a[].y >> a[].x >> a[].y >> a[].x >> a[].y >> a[].x >> a[].y >>
b[].x >> b[].y >> b[].x >> b[].y >> b[].x >> b[].y >> b[].x >> b[].y ) {
memset( vis, , sizeof(vis) );
if( ok() ) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return ;
}

CF993A Two Squares 几何 第二道 暴力或判断条件(*)的更多相关文章

  1. Linux学习第二道坎——系统目录结构及其作用

    如果说Linux学习的第一道坎是系统安装及对磁盘分区的理解,那么第二道坎就应该是对Linux系统目录结构及其作用的掌握了(这里主要指根目录 / 下的一级目录)! 随着Linux的不断发展,Linux的 ...

  2. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3)(A.B.C,3道暴力题,C可二分求解)

    A. Is it rated? time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  3. 2015年上海现场赛重现 (A几何, K暴力搜索)

    A: 题目链接 :https://vjudge.net/contest/250823#problem/A 参考 : https://www.cnblogs.com/helenawang/p/54654 ...

  4. poj 2002 Squares 几何二分 || 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 5749 Descript ...

  5. 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道——最小分割分数

    题目: 给 n 个正整数 a_1,…,a_n, 将 n 个数顺序排成一列后分割成 m 段,每一段的分数被记为这段内所有数的和,该次分割的分数被记为 m 段分数的最大值.问所有分割方案中分割分数的最小值 ...

  6. 今日头条 2018 AI Camp 6 月 2 日在线笔试编程题第二道——两数差的和

    题目 给 n 个实数 a_1, a_2 ... a_n, 要求计算这 n 个数两两之间差的绝对值下取整后的和是多少. 输入描述 第一行为一个正整数 n 和一个整数 m.接下来 n 行,第 i 行代表一 ...

  7. nyoj 7 街区最短路径问题 (曼哈顿距离(出租车几何) or 暴力)

    街区最短路径问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 一个街区有很多住户,街区的街道只能为东西.南北两种方向. 住户只可以沿着街道行走. 各个街道之间的间 ...

  8. POJ 2002 Squares 几何, 水题 难度: 0

    题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...

  9. 5 微信票据 access_token--开发微信的第二道坎儿

    一 access_token基本概念 定义:access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存. 时效性:access_ ...

随机推荐

  1. 命令用法习题,yum仓库的创建 chapter02 - 03 作业

    1.  分别用cat \tac\nl三个命令查看文件/etc/ssh/sshd_config文件中的内容,并用自己的话总计出这三个文档操作命令的不同之处? [root@localhost /]# ca ...

  2. c语言指针汇总

    1.指向单个变量的指针: ; int* p = &a; printf("%d", *p); 2.数组的指针 (1)一维数组的指针 ] = { ,,,, }; int *p; ...

  3. c#实现深拷贝的几种方法

    为什么要用到深拷贝呢?比如我们建了某个类Person,并且实例化出一个对象,然后,突然需要把这个对象复制一遍,并且复制出来的对象要跟之前的一模一样,来看下我们一般会怎么做,看代码 public cla ...

  4. Go和Python学习计划

    计划虽然不一定能实现,但还是要有的,万一实现了呢. 一.学习Go 1.先看尚雪谷https://www.bilibili.com/video/av48141461/?p=12的go语言全套,把基础的过 ...

  5. Go中的interface学习

    学过Java的同学都知道在Java中接口更像是一种规范,用接口定义了一组方法,下面实现这个接口的类只管按照写好的方法名和返回值去实现就好,内部如何实现是各个方法自己的事情,接口本身不关注. 另外Jav ...

  6. 渐进式web应用开发---使用indexedDB实现ajax本地数据存储(四)

    在前几篇文章中,我们使用service worker一步步优化了我们的页面,现在我们学习使用我们之前的indexedDB, 来缓存我们的ajax请求,第一次访问页面的时候,我们请求ajax,当我们继续 ...

  7. Mysql 局域网连接设置——Windows

    在公司工作中,会遇到mysql数据库存储于某个人的电脑上,大家要想连接mysql服务,装有mysql服务的电脑就必须开启远程连接. 其实不仅仅是局域网,只要你有数据库所在服务器的公网IP地址都能连上. ...

  8. 如何获取app中的toast

    前言 Toast是什么呢?在这个手机飞速发展的时代,app的种类也越来越多,那们在日常生活使用中,经常会发现,当你在某个app的输入框输入非法字符或者非法执行某个流程时,经常看到系统会给你弹出一个黑色 ...

  9. WPF中TimeSpan的坑

    记一次在WPF中,在将格式为“DD.HH:mm:ss”字符串转换成TimeSpan时遇到的坑 如果字符串为:DD.HH:mm:ss,转换结果正确.例如: var currentValue = &quo ...

  10. MTFlexbox自动化埋点探索

    1. 背景 跨平台动态化技术是目前移动互联网领域的重点关注方向,它既能节约人力,又能实现业务快速上线的需求.经过十年的发展,美团App已经变成了一个承载众多业务的超级平台,众多的业务方对业务形态的快速 ...