UVa 201 Squares
题意:
给出这样一个图,求一共有多少个大小不同或位置不同的正方形。
分析:
这种题一看就有思路,最开始的想法就是枚举正方形的位置,需要二重循环,枚举边长一重循环,判断是否为正方形又需要一重循环,复杂度为O(n4),对于n≤9来说,这个复杂度可以接受。
可以像预处理前缀和那样,用O(1)的时间判断是否为正方形,这样总的复杂度就优化到O(n3)。
这个方法转自这里
We can think that vertical or horizontal lines are edges between two adjecent point. After that we can take a three dimensional array (say a [N][N][2]) to store the count of horizontal(a[i][j][0]) edges and vertical(a[i][j][1]) edges. a[i][j][0] contains number of horizontal edges at row i upto coloumn j. and a[i][j][1] contains number of vertical edges at coloumn j upto row i. Next you use a O(n^2) loop to find a square. a square of size 1 is found if there is an edge from (i,j) to (i,j+1) and (i,j+1) to (i+1,j+1) and (i,j) to (i+1,j) and (i+1,j) to (i+1,j+1) we can get this just by subtracting values calculated above.
举个例子,a[i][j][0]表示在第i行上,从第一列到第j列水平边数,如果a[i][j+l][0] - a[i][j][0],说明点(i, j)到(i, j+l)有一条长为l的水平线段。
我还被输入坑了,注意VH后面,哪个数代表行,哪个数代表列。
- #include <cstdio>
- #include <cstring>
- const int maxn = ;
- bool G[][maxn][maxn];
- int a[][maxn][maxn], cnt[maxn];
- int main()
- {
- //freopen("in.txt", "r", stdin);
- int n, m, kase = ;
- while(scanf("%d", &n) == && n)
- {
- memset(G, false, sizeof(G));
- memset(a, , sizeof(a));
- memset(cnt, , sizeof(cnt));
- scanf("%d", &m);
- getchar();
- for(int k = ; k < m; ++k)
- {
- char c;
- int i, j;
- scanf("%c %d %d", &c, &i, &j);
- getchar();
- if(c == 'H') G[][i][j+] = true;
- else G[][j+][i] = true;
- }
- for(int i = ; i <= n; ++i)
- for(int j = ; j <= n; ++j)
- {
- a[][i][j] = a[][i][j-] + G[][i][j];
- a[][i][j] = a[][i-][j] + G[][i][j];
- }
- for(int i = ; i < n; ++i)
- for(int j = ; j < n; ++j) //枚举正方形的左上角
- for(int l = ; i+l<=n && j+l<=n; ++l) //枚举正方形的边长
- if(a[][i][j+l]-a[][i][j] == l && a[][i+l][j+l]-a[][i+l][j] == l
- && a[][i+l][j]-a[][i][j] == l && a[][i+l][j+l]-a[][i][j+l] == l)
- cnt[l]++;
- if(kase) printf("\n**********************************\n\n");
- printf("Problem #%d\n\n", ++kase);
- bool flag = false;
- for(int i = ; i <= n; ++i) if(cnt[i])
- {
- printf("%d square (s) of size %d\n", cnt[i], i);
- flag = true;
- }
- if(!flag) puts("No completed squares can be found.");
- }
- return ;
- }
代码君
UVa 201 Squares的更多相关文章
- 【每日一题】Squares UVA - 201 暴力+输出坑 + 读文件模板
题意 给你n*n的图,让你数正方形 题解:暴力for每个点,对于每个点从它出发顺时针走一个正方形.走完就ans[i]++; 坑:多输了一行******,然后在那里手摸样例,无限debug orz #d ...
- 【UVA】201 Squares(模拟)
题目 题目 分析 记录一下再预处理一下. 代码 #include <bits/stdc++.h> int main() { int t=0,s,n; while(scanf ...
- Squares UVA - 201
A children's board game consists of a square array of dots that contains lines connecting some of th ...
- UVa 1453 - Squares 旋转卡壳求凸包直径
旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...
- uva 1453 - Squares
旋转卡壳算法: 直接在这个上面粘的模板 主要用途:用于求凸包的直径.宽度,两个不相交凸包间的最大距离和最小距离··· 这题就是求凸包的直径 #include <cstdio> #inclu ...
- UVA 4728 Squares(凸包+旋转卡壳)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...
- UVa 1643 Angle and Squares
题意: 如图,有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 分析: 直观上来看,当这n个正方形的对角线在一条直线上时,封闭区域的面积最大.( ...
- UVA 12113 Overlapping Squares
题意: 总共有6个2*2的正方形,判断是否能够成所给的形状. 思路: 一个正方形总共有9种摆放方式,对于整个地图来说摆放方式总共有2的9次方种摆放方式.然后将地图用9*5的数组表示,正方形的位置用其8 ...
- UVa 1643 Angle and Squares (计算几何)
题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...
随机推荐
- phpcms常用接口调用方法
常用函数 , 打开include/global.func.php,下面存放一些公共函数 view plaincopy to clipboardprint?function str_charset($i ...
- CorelDRAW 二维码插件
随着智能手机的流行,二维码在各个领域大量应用,这个插件在补CorelDRAW这方面的不足: 这个插件是 cpg 格式,安装请看这篇博客:http://www.cnblogs.com/o594cql/p ...
- jQuery基础与实例
一.简介 1.什么是jQuery jQuery是一个轻量级.快速简洁的javaScript库,能让我们方便快捷的选择元素操作元素属性. 2.下载地址 3.jQuery使用方式 $("div& ...
- max_size, capacity and size 的区别
The max_size() function returns the maximum number of elements that the container can hold. The max_ ...
- 针对PIL中ImageDraw.py报错的解决方案
linux mint 13开始就发现这个问题了,一直不知道怎么解决,今天突然发现了解决方案,来分享给大家 下面是修改对比,自己根据修改,这个是系统文件,需要root权限,路径/usr/lib/pyth ...
- 长安铃木经销商爬取(解析xml、post提交、python中使用js代码)
1.通过火狐浏览器,查找大长安铃木官网中关于经销商的信息主要在两个网页中 http://www.changansuzuki.com/khfw/xml/pro.xml 地域信息 http://www. ...
- Oracle表连接
一个普通的语句select * from t1, t2 where t1.id = t2.id and t1.name = 'a'; 这个语句在什么情况下最高效? 表连接分类: 1. 嵌套循环连接(N ...
- Hibernate从入门到精通(八)一对多单向关联映射
上次的博文Hibernate从入门到精通(七)多对一单向关联映射我们主要讲解了一下多对一单向关联映射,这次我们继续讲解一下一对多单向映射. 一对多单向关联映射 在讲解一对多单向关联之前,按照我们的惯例 ...
- 类的本质、description方法、SEL、NSLog输出增强
一.类的本质 1.类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class *Class; 类名就代表着类对象 ...
- owa Your request can't be completed right now. Please try again later.
Your request can't be completed right now. Please try again later.