计算几何,CF 993A - Two Squares

目录

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解


一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

A - Two Squares

二、解题报告

1、思路分析

由于数据量很小,可以暴力判断一个正方形的每个点是否在另一个正方形内

数据量大的话,可以套用计算几何中 判断线段相交 的方式

由于本题是正方形,只不过第二个正方形旋转了45°,那么我们可以考虑更简单的做法(因为判断线段相交也是有一些corner case的)

对于(x, y) ,顺时针旋转45°后为((x + y) / sqrt(2), (x - y) / sqrt(2))

然后判断square1 每个点是否在square2 中(二者需要旋转)

然后判断square2 每个点是否在square1 中(二者不需要旋转)

但这样并非是充分的,下面这个特判跟前段时间力扣逆天3100分计算几何题类似

我们判断下square1 的中心是否在square2中,比如下面这个情况

证明充分性其实也好证明,因为一个是正方形,一个是旋转45°正方形,如果二者相交且没有任何一个正方形的一个点被另一个包含,我们无法构造出中心不被包含的情况的

2、复杂度

时间复杂度: O(1)空间复杂度:O(1)

3、代码详解

 ​
#include <bits/stdc++.h>

using i64 = long long;
using i32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;

constexpr int inf32 = 1E9 + 7;
constexpr i64 inf64 = 1E18 + 7;

void solve() {
	std::array<int, 8> s1, s2;
	// U D L R
	std::array<int, 4> dir1, dir2;
	dir1[0] = dir2[0] = -inf32, dir1[1] = dir2[1] = inf32, 
	dir1[2] = dir2[2] = inf32, dir1[3] = dir2[3] = -inf32;

	for (int i = 0; i < 4; ++ i) {
		std::cin >> s1[i * 2] >> s1[i * 2 + 1];
		dir1[0] = std::max(dir1[0], s1[i * 2 + 1]);
		dir1[1] = std::min(dir1[1], s1[i * 2 + 1]);
		dir1[2] = std::min(dir1[2], s1[i * 2]);
		dir1[3] = std::max(dir1[3], s1[i * 2]);
	}

	for (int i = 0; i < 4; ++ i) {
		std::cin >> s2[i * 2] >> s2[i * 2 + 1];
		dir2[0] = std::max(dir2[0], s2[i * 2] - s2[i * 2 + 1]);
		dir2[1] = std::min(dir2[1], s2[i * 2] - s2[i * 2 + 1]);
		dir2[2] = std::min(dir2[2], s2[i * 2] + s2[i * 2 + 1]);
		dir2[3] = std::max(dir2[3], s2[i * 2] + s2[i * 2 + 1]);
	}


	for (int i = 0; i < 4; ++ i) {
		int x = s1[i * 2], y = s1[i * 2 + 1];
		if (x + y >= dir2[2] && x + y <= dir2[3] && x - y <= dir2[0] && x - y >= dir2[1]) {
			std::cout << "YES\n";
			return;
		}

		x = s2[i * 2], y = s2[i * 2 + 1];
		if (x >= dir1[2] && x <= dir1[3] && y <= dir1[0] && y >= dir1[1]) {
			std::cout << "YES\n";
			return;
		}
	}

	int x = 0, y = 0;

	for (int i = 0; i < 4; ++ i) {
		x += s1[i * 2], y += s1[i * 2 + 1];
	}

	x /= 4, y /= 4;

	if (x + y >= dir2[2] && x + y <= dir2[3] && x - y <= dir2[0] && x - y >= dir2[1]) {
		std::cout << "YES\n";
		return;
	}

	x = 0, y = 0;

	for (int i = 0; i < 4; ++ i) {
		x += s2[i * 2], y += s2[i * 2 + 1];
	}

	std::cout << "NO\n";
}

auto FIO = []{
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);
	return 0;
}();

int main () {
	#ifdef DEBUG
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	#endif
	
	int T = 1;
	// std::cin >> T;
	while (T --)
		solve();

	return 0;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[1\]: Curves and Surfaces A Bidirectional Generating Algorithm for Rational Parametric Curves(Z. Li, L. Ma)Fast Detection of the Geometric Form of Two-Dimensional Cubic Bézier Curves(S. Vincent)Exact Evaluation of Subdivision Surfaces(eigenstructures for Catmull-Clark and Loop schemes) (J. Stam)Exact Evaluation of Catmull-Clark Subdivision Surfaces near B-Spline Boundaries(D. Lacewell, B. Burley)Smooth Two-Dimensional Interpolations: A Recipe for All Polygons(E. Malsch, J. Lin, G. Dasgupta) Normal Patches / PN-Triangles(R. Stimpson)Marching Cubes(.vol files) (R. Stimpson)Coons Patches(R. Stimpson)Exact Catmull-Clark Subdivision evaluation(and mean-curvature minimization) (F. Hecht)Laplacian Surface Editing(2D curve deformation) (O. Sorkine, D. Cohen-Or, Y. Lipman, M. Alexa, C. Roessl, H.-P. Seidel)Elasticurves: Exploiting Stroke Dynamics and Inertia for the Real-time Neatening of Sketched 2D Curves(Y. Thiel, K. Singh, R. Balakrishnan) 。 引用\[2\]: Segmentation efpisoft: hierarchical mesh segmentation based on fitting primitives(M. Attene)mesh segmentation benchmark database and viewer(X. Chen, A. Golovinskiy, T. Funkhouser)Graphite(variational shape approximation,image vectorization) \[documentation wiki\] (Authors)SegMatch: Shape Segmentation and Shape Matching from Point Cloud(T. Dey, S. Goswami)ShapeAnnotatorsegmentation tool (fitting primitives, barycentric/height/integral geodesic Morse, Plumber, Lloyd clustering)(Authors)Shape Diameter Function (SDF) segmentation tool(L. Shapira) 。 引用\[3\]:DP。对于每个'#'来说,要使图美丽就要保证每个'#'的正下方到正右下都填满'#' ....#. ....#. ...... ....## .#.... -> .#..## (题解里CV过来的) ...... .##.## ...... .##### 。 问题: Defect-free Squares是什么意思? 回答: Defect-free Squares是指在一个图形中,每个'#'的正下方到正右下方都填满了'#',从而使整个图形看起来没有缺陷。这个概念可以通过动态规划(DP)来实现,对于每个'#',需要保证其正下方到正右下方都填满了'#',从而形成一个完整的正方形。\[3\] #### 引用[.reference_title] - *1* *2* [图形学领域的关键算法及源码链接](https://blog.csdn.net/u013476464/article/details/40857873)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ABC311 A-F](https://blog.csdn.net/Muelsyse_/article/details/131873631)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值