CF993A Two Squares 几何 第二道 暴力或判断条件(*)
1 second
256 megabytes
standard input
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.
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.
Print "Yes" if squares intersect, otherwise print "No".
You can print each letter in any case (upper or lower).
0 0 6 0 6 6 0 6
1 3 3 5 5 3 3 1
YES
0 0 6 0 6 6 0 6
7 3 9 5 11 3 9 1
NO
6 0 6 6 0 6 0 0
7 4 4 7 7 10 10 7
YES
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 几何 第二道 暴力或判断条件(*)的更多相关文章
- Linux学习第二道坎——系统目录结构及其作用
如果说Linux学习的第一道坎是系统安装及对磁盘分区的理解,那么第二道坎就应该是对Linux系统目录结构及其作用的掌握了(这里主要指根目录 / 下的一级目录)! 随着Linux的不断发展,Linux的 ...
- 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 ...
- 2015年上海现场赛重现 (A几何, K暴力搜索)
A: 题目链接 :https://vjudge.net/contest/250823#problem/A 参考 : https://www.cnblogs.com/helenawang/p/54654 ...
- poj 2002 Squares 几何二分 || 哈希
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 5749 Descript ...
- 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道——最小分割分数
题目: 给 n 个正整数 a_1,…,a_n, 将 n 个数顺序排成一列后分割成 m 段,每一段的分数被记为这段内所有数的和,该次分割的分数被记为 m 段分数的最大值.问所有分割方案中分割分数的最小值 ...
- 今日头条 2018 AI Camp 6 月 2 日在线笔试编程题第二道——两数差的和
题目 给 n 个实数 a_1, a_2 ... a_n, 要求计算这 n 个数两两之间差的绝对值下取整后的和是多少. 输入描述 第一行为一个正整数 n 和一个整数 m.接下来 n 行,第 i 行代表一 ...
- nyoj 7 街区最短路径问题 (曼哈顿距离(出租车几何) or 暴力)
街区最短路径问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 一个街区有很多住户,街区的街道只能为东西.南北两种方向. 住户只可以沿着街道行走. 各个街道之间的间 ...
- POJ 2002 Squares 几何, 水题 难度: 0
题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...
- 5 微信票据 access_token--开发微信的第二道坎儿
一 access_token基本概念 定义:access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存. 时效性:access_ ...
随机推荐
- golang-http 请求---设置header与直接发
背景 现在各种软件到处都是,写代码难免有时候需要http 调用其他的接口. 其实这个东西还挺常用,虽然很简单,但是写的时候 又忘,就像是提笔忘字,索性总结一下吧. 不需要设置header属性的http ...
- Oracle DBLink跨数据库访问SQL server数据同步 踩坑实录
项目需求:这里暂且叫A公司吧,A公司有一套人事管理软件,需要与我们公司的软件做人员信息同步,A公司用的是SQL server数据库,我们公司用的Oracle,接口都不会开发(一万句"fuck ...
- GridView 使用详解
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- 如何编写一个WebPack的插件原理及实践
_ 阅读目录 一:webpack插件的基本原理 二:理解 Compiler对象 和 Compilation 对象 三:插件中常用的API 四:编写插件实战 回到顶部 一:webpack插件的基本原理 ...
- java8(二)方法引用
方法引用让你可以重复使用现有的方法定义,并像 Lambda 一样进行传递. 方法引用可以被看作仅仅调用特定方法的 Lambda 的一种快捷写法. 事实上,方法引用就是让你根据已有的方法实现来创建 La ...
- VS调试时修改代码
最近碰到一个问题,就是vs在调试模式下无法修改代码之后再继续,这种严重影响工作效率的问题怎么能忍,所以决心把这个坑填满.网上搜了大堆有头无尾有尾无头的答案,我一个一个试了几乎都没啥用.最后通过不断的测 ...
- H5中的history方法Api介绍
最近公司在做一个微信公众号,看了项目源码,看到项目中用到了history的Api来进行控制浏览器的历史记录及前进/后退键: 下面来跟大家一起来捋捋history的Api方法和使用: history.p ...
- javaScript基础-02 javascript表达式和运算符
一.原始表达式 原始表达式是表达式的最小单位,不再包含其他表达式,包含常量,直接量,关键字和变量. 二.对象和数组的初始化表达式 对象和数组初始化表达式实际上是一个新创建的对象和数组. 三.函数表达式 ...
- python2.7官方文档阅读笔记
官方地址:https://docs.python.org/2.7/tutorial/index.html 本笔记只记录本人不熟悉的知识点 The Python Tutorial Index 1 Whe ...
- Javarscipt中数组或者字符串的随机排序方法
在日常开发中,经常会遇到随机排序的需求,思路就是利用Math.random()方法,抽取随机数,让数组中的元素进行对调: 话不多说直接上代码,方法一:基本思路就是将a中随机抽取一个元素,放入b中,再从 ...