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_ ...
随机推荐
- Superset 官方入门教程中文翻译
本文翻译自 Superset 的官方文档:Toturial - Creating your first dashboard 最新版本的 Superset 界面与功能上与文档中提到的会有些许出入,以实际 ...
- 利用模板生成html页面(NVelocity)
公司的网站需要有些新闻,每次的新闻格式都是一样的,而不想每次都查询操作,所以想把这些新闻的页面保存成静态的html,之后搜索了下就找到了这个模板引擎,当然其他的模板引擎可以的,例如:Razor,自己写 ...
- 4如何用PHP给MySQL数据库添加记录
首先连接数据库(依旧用第二篇的方法) 假设数据库表里只有id,name,email三列 添加以下代码 $inputemail=写你要的email;$inputname=写你要的name;//先设定你要 ...
- 什么是Singleton?
Singleton:在Java中即指单例设计模式,它是软件开发中最常用的设计模式之一. 单:指唯一 例:指实例 单例设计模式,即某个类在整个系统中只能有一个实例对象可被获取和使用的代码模式. 要点: ...
- MySQL高速缓存
MySQL高速缓存启动方法及参数详解query_cache_size=32M query_cache_type=1,默认配置下,MySQL的该功能是没有启动的,可能你通过show variables ...
- 编码规范 | Java函数优雅之道(下)
上文背景 本文总结了一套与Java函数相关的编码规则,旨在给广大Java程序员一些编码建议,有助于大家编写出更优雅.更高质.更高效的代码. 内部函数参数尽量使用基础类型 案例一:内部函数参数尽量使用基 ...
- 洛谷 P3648 [APIO2014]序列分割
题意简述 有一个长度为n的序列,分成k + 1非空的块, 选择两个相邻元素把这个块从中间分开,得到两个非空的块. 每次操作后你将获得那两个新产生的块的元素和的乘积的分数.求总得分最大值. 题解思路 f ...
- 编程使用c#连接到IBM db2的两种方式
一:使用c#通过odbc连接到IBM db2使用 ConnectionString 属性连接到各种数据源. 部署:只要在客户端安装IBM DB2 ODBC driver.配置DSn即可. 1):可以单 ...
- 解决Sklearn中使用数据集MNIST无法获取的问题(WinError 10060)
今天在学习PCA的时候,使用mnist数据集遇到一个问题,代码是这样的: import numpy as np from sklearn.datasets import fetch_mldata mn ...
- C# 一个计算器功能实现引发的思考
一.需求 计算器功能需求,这个众所周知,很明确了. 二.步骤分析 1)初级实现计算器 static int Calculator(int a,int b,string str) { switch(st ...