UVA - 201 Squares

本文提供了一种解决UVA-201Squares问题的方法,通过构建二维地图并遍历检查所有可能的正方形组合来确定特定大小的完整正方形是否存在。代码使用C++实现。

UVA - 201 Squares

(1)把每相邻两点之间插入一个坐标表示边做一个2*n-1的map
(2)点之间的边用‘1’表示连接


#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"string.h"
using namespace std;
char map[18][18];
int n, m;
bool isout(int x,int y,int i)
{
	if (x + i < 2 * n && y + i < 2 * n)return false;
	return true;
}
bool find(int l)
{
	int cnt = 0;
	for (int i = 1; i < 2 * n; i += 2)
	{
		for (int j = 1; j < 2 * n; j += 2)
		{
			int is = 1;
			int sq = 2 * l;
			if (isout(i, j, sq))continue;//正方形越界直接判断下一个点
			for (int k = 1; k < 2 * l; k += 2)
			{
				if (map[i][j + k] != '1' || map[i + k][j] != '1' || map[i + sq][j + k] != '1' || map[i + k][j + sq] != '1')
				{
					is = 0; break;
				}
			}
			if (is)cnt++;
		}
	}
	if (cnt)
	{
		printf("%d square (s) of size %d\n", cnt, l);
		return true;
	}
	return false;
}
int main()
{
	int flag, time = 0;
	while (cin >> n >> m && ++time)
	{
		int x, y; char hv;
		memset(map, ' ', sizeof(map));
		/*for (int i = 1; i < 2 * n; i+=2)
			for (int j = 1; j < 2 * n; j+=2)
				map[i][j] = '*';
				把点标记成‘*’,输出map可以看得更直观*/
		for (int i = 0; i < m; i++)
		{
			cin >> hv >> x >> y;
			if (hv == 'H')map[2* x - 1][2 * y] = '1';//把‘1’改成‘-’可以在map上看得更直观
			else map[2 * y][2 * x - 1] = '1';
		}
		flag = 0;
		if (time != 1)printf("\n**********************************\n\n");
		printf("Problem #%d\n\n", time);
		for (int i = 1; i <= n; i++)
			if (find(i))flag = 1;
		if (!flag)printf("No completed squares can be found.\n");
	}
	return 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.youkuaiyun.com/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.youkuaiyun.com/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 ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值