博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 993 A
阅读量:4549 次
发布时间:2019-06-08

本文共 1870 字,大约阅读时间需要 6 分钟。

codeforces 993 A

introduction

判断两个正方形是否相交(只有一个有公共点也算相交),其中一个平放,另一个倾斜45°。

method

方法有很多,从直观上都可以理解。但我的方法太麻烦了,失败了。

觉得题解的思路很好,所以记录一下。
为了便于描述,平放的正方形记为A,倾斜45°的记为B
相交可以分为4种情况:

  1. B的角落在A
  2. A的角落在B
  3. B的中心落在A
  4. A的中心落在B

第一种情况好判断,按照是两个平放的正方形来处理。难点是AB中的情况。

一个想法是将A旋转45°,B也旋转45°,这样就转化成第一种情况。但是这样做的代价是引入了浮点数。题解的巧妙之处就是观察到了放缩是不会改变相交性的。

replace each ? coordinate with ?+? and each ? coordinate with ?−?

这样就完全转化成第一种情况

其他的思路

点都是整点,并且每个正方形最多是100×100,可以枚举两个正方形是否有公共点

tips

  1. 放缩是不会改变相交性的
  2. 两个平放正方形的相交性

reference

code

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEBUG(x) cout<<#x<<" = "<
<
>(istream &in,square &s) { for(int i=0;i<4 ;i++ ){ in>>s.x[i]>>s.y[i]; if(i==0)s.mxx=s.mix=s.x[i],s.mxy=s.miy=s.y[i]; else { s.mxx=max(s.mxx,s.x[i]); s.mix=min(s.mix,s.x[i]); s.mxy=max(s.mxy,s.y[i]); s.miy=min(s.miy,s.y[i]); } } return in; } friend ostream & operator<<(ostream &out,square &s) { for(int i=0;i<4 ;i++ ){ out<
<<" "<
<<" "; } }};square roughrotate(square s){ square rt; for(int i=0;i<4 ;i++ ){ rt.x[i]=s.x[i]+s.y[i]; rt.y[i]=s.x[i]-s.y[i]; if(i==0)rt.mxx=rt.mix=rt.x[i],rt.mxy=rt.miy=rt.y[i]; else { rt.mxx=max(rt.mxx,rt.x[i]); rt.mix=min(rt.mix,rt.x[i]); rt.mxy=max(rt.mxy,rt.y[i]); rt.miy=min(rt.miy,rt.y[i]); } } return rt;}bool isin(square p,square d){ for(int i=0;i<4 ;i++ ){ if(d.x[i]>=p.mix&&d.x[i]<=p.mxx &&d.y[i]>=p.miy&&d.y[i]<=p.mxy)return true; } int mdx=(d.mix+d.mxx)/2, mdy=(d.miy+d.mxy)/2; if(mdx>=p.mix&&mdx<=p.mxx&&mdy>=p.miy&&mdy<=p.mxy)return true; return false;}int main(){// freopen("in.txt","r",stdin); square par,deg; cin>>par>>deg; bool b1=isin(par,deg); square par1=roughrotate(deg); square deg1=roughrotate(par); bool b2=isin(par1,deg1); if(b1||b2)cout<<"YES"<

转载于:https://www.cnblogs.com/MalcolmMeng/p/10200788.html

你可能感兴趣的文章
SublimeText3常用操作
查看>>
008---re正则模块
查看>>
onchange of select
查看>>
Mybatis 分页
查看>>
bmap
查看>>
设计模式的介绍
查看>>
It’s Not Too Late to Learn How to Code
查看>>
看看别人十年软件开发后学到了什么
查看>>
Python的平凡之路(19)
查看>>
数据分析---《Python for Data Analysis》学习笔记【03】
查看>>
ACM练习网站
查看>>
输入输出外挂(纯数字型)
查看>>
限制输出字数,超过的用...省略
查看>>
bnuoj25660 Two Famous Companies
查看>>
股票投资
查看>>
C# 启动另一个程序
查看>>
木桶效应
查看>>
springMVC3学习(二)--ModelAndView对象
查看>>
postgres前言(常用语句熟悉 系列一)
查看>>
Windows 上 GitHub Desktop 的操作[转]
查看>>