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个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...
随机推荐
- HTMLEncode httpencode UTF8Encode
1.引用单元: httpApp; 2. 对于 http Post的提交内容,应该是: HttpEncode(Utf8Encode(StrValue)); 不然与web方式的 Url_enco ...
- 一个例子说明如何在DataSnap中使用FireDAC
一.FireDAC调用DataSnap远程方法查询数据示例 1.服务端使用FDQUERY查询数据并返回TDATASET: function TServerMethods1.GetData(var sq ...
- [转]java gridbag 说明
gridx = 2; // X2 gridy = 0; // Y0 gridwidth = 1; // 横占一个单元格 gridheight = 1; // 列占一个单元格 weightx = 0.0 ...
- Android Studio创建项目
创建项目 首先,先指出Android Studio中的两个概念. Project 和 Module .在Android Studio中, Project 的真实含义是工作空间, Module 为一个具 ...
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...
- oracle新建用户
说明:以下命令在PLSQL中运行 一.以管理员身份登录PLSQL scott/root as sysdba 二.创建新用户 create user extjsTest1 identified by r ...
- js中批量处理样式——cssText的使用
http://www.cnblogs.com/snandy/archive/2011/03/12/1980444.html
- PYTHON设计模式,创建型之简单工厂模式
这个系统,感觉思路清爽,,相信多练练,多思考,就会熟悉的.. http://www.jianshu.com/p/2450b785c329 #!/usr/bin/evn python #coding:u ...
- java 用JNA方法调用C++动态链接库
JNA(Java Native Access)框架是一个开源的Java框架,是SUN公司主导开发的,建立在经典的JNI的基础之上的一个框架.非常强大.易用,功能上类似与.NET的P/Invoke.你只 ...
- Maven中聚合与继承
何为继承? --继承为了消除重复,我们把很多相同的配置提取出来 --例如:grouptId,version等 就像写java程序一样,对于有共性切重复的东西,就提取出来. 如有三个pom.xml配 ...