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个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...
随机推荐
- java之StringBuffer
StringBuffer就是字符串缓冲区,用于存储数据的容器. 特点:长度可变,可存储不同类型的数据,最终转化成字符串使用,可以对字符串修改 功能: 添加:append(value), insert( ...
- CentOS 下 Codeblocks 的 安装 + 汉化 以及 基本使用介绍
Codeblocks 安装 注:在root用户下运行下列命令 1.安装gcc,需要c和c++两部分,默认安装下,CentOS不安装编译器的,在终端输入以下命令即可 yum install gcc yu ...
- PIL 安装
1.安装依赖包 1.1 ubuntu安装 apt-get install python-devapt-get install libjpeg-dev apt-get install libjpeg8- ...
- jQuery 的 json 格式的处理问题
在实际的使用中时常会发生一些ajax验证的诡异事件,如我在一个文件中使用json传送数据,结果出现了当数据发送到服务器端时(后端主要呈现的是对json数据对象的数据库查询操作),结果显示的是数据已经插 ...
- Oracle表连接
一个普通的语句select * from t1, t2 where t1.id = t2.id and t1.name = 'a'; 这个语句在什么情况下最高效? 表连接分类: 1. 嵌套循环连接(N ...
- MVC学习系列——参考
C#进阶系列——WebApi接口传参不再困惑:传参详解 http://www.cnblogs.com/landeanfen/p/5337072.html
- gif格式的图片不能存在与包含js目录的路径中?
如题:gif格式的图片不能存在与包含js目录的路径中?是我的设置问题?还是真不能存在于js目录中. 今天纠结了一下午,某个项目中的效果就是出不来,找了差不多两个半小时... 在D盘新建一个js和jss ...
- 使用Yeoman搭建 AngularJS 应用 (4) —— 让我们搭建一个网页应用
在开发一个的网页传统工作流程中,你需要大量的时间去设置引用文件,下载依赖文件,并且手动的创建网页文件结构.Yeoman生成器将会帮助你完成这些.让我们安装一个AngularJS项目的生成器. 安装An ...
- 1021: [SHOI2008]Debt 循环的债务 - BZOJ
Description Alice.Bob和Cynthia总是为他们之间混乱的债务而烦恼,终于有一天,他们决定坐下来一起解决这个问题.不过,鉴别钞票的真伪是一件很麻烦的事情,于是他们决定要在清还债务的 ...
- httpclient 302 重定向
主要是由于页面可能不是通过 request.sendRedirect跳转的,可能通过js location跳转的.所以需要拿跳转后的 地址,重新发送请求...如下所示 if (status == H ...