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_ ...
随机推荐
- oracle的开窗函数
原创 select * from (select province, commodity, sum(price), ROW_NUMBER() OVER(PARTITION BY province o ...
- 深入理解JVM-类加载器深入解析(2)
深入理解JVM-类加载器深入解析(2) 加载:就是把二进制形式的java类型读入java虚拟机中 连接: 验证: 准备:为类变量分配内存,设置默认值.但是在到达初始化之前,类变量都没有初始化为真正的初 ...
- python_0基础学习_day01
Python是一门动态解释型的强类型定义语言 一.变量 变量命名规则 由数字.字母.下划线组成 不能以数字开头 要具有描述性 要区分大小写 禁止使用python的关键字(在pycharm中关键字明明变 ...
- snort规则中byte_test参数详解
例子: byte_test:4,>,1000,20 这里是从本规则内前面匹配的位置结尾开始,向后偏移20个字节,再获取后面的4个字节的数据,与十进制数据1000进行比较,如果大于1000,就命中 ...
- Linux(Ubuntu)安装Swift和Swiftlint
很多时候iOS开发完毕需要接入CI中,而很多CI是基于Linux的,需要在Linux平台安装Swift和Swiftlint,下面就是针对这两个软件的安装步骤. Swift安装 环境 系统:Ubuntu ...
- 伪分布式Spark + Hive on Spark搭建
Spark大数据平台有使用一段时间了,但大部分都是用于实验而搭建起来用的,搭建过Spark完全分布式,也搭建过用于测试的伪分布式.现在是写一遍随笔,记录一下曾经搭建过的环境,免得以后自己忘记了.也给和 ...
- vue面试题整理vuejs基础知识整理
初级参考 1.v-show 与 v-if 区别 v-show 是css隐藏,v-if是直接销毁和创建,所以频繁切换的适合用v-show 2.计算属性和 watch 的区别 计算属性是自动监听依赖值的变 ...
- SVN服务器更改ip地址客户端怎么设置
SVN 服务器 IP 地址修改后,客户端对服务器的连接可以采用以下的方法重定位: 1. 如果客户端工具是TortoiseSVN,直接在工作副本上右键,选择TortoiseSVN->relocat ...
- 从零开始实现ASP.NET Core MVC的插件式开发(四) - 插件安装
标题:从零开始实现ASP.NET Core MVC的插件式开发(四) - 插件安装 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/11260750. ...
- 我的C语言学习1
学习是快乐的,尤其是从之前看到一个程序的一头雾水到大致懂了是怎么回事,这个过程是兴奋开心的,让我不断的前进,不能自拔,今天就要结束,总结一下. 1.1-第一个C语言 #include<stdio ...