题意:

给出这样一个图,求一共有多少个大小不同或位置不同的正方形。

分析:

这种题一看就有思路,最开始的想法就是枚举正方形的位置,需要二重循环,枚举边长一重循环,判断是否为正方形又需要一重循环,复杂度为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的更多相关文章

  1. 【每日一题】Squares UVA - 201 暴力+输出坑 + 读文件模板

    题意 给你n*n的图,让你数正方形 题解:暴力for每个点,对于每个点从它出发顺时针走一个正方形.走完就ans[i]++; 坑:多输了一行******,然后在那里手摸样例,无限debug orz #d ...

  2. 【UVA】201 Squares(模拟)

    题目 题目     分析 记录一下再预处理一下.     代码 #include <bits/stdc++.h> int main() { int t=0,s,n; while(scanf ...

  3. Squares UVA - 201

    A children's board game consists of a square array of dots that contains lines connecting some of th ...

  4. UVa 1453 - Squares 旋转卡壳求凸包直径

    旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...

  5. uva 1453 - Squares

    旋转卡壳算法: 直接在这个上面粘的模板 主要用途:用于求凸包的直径.宽度,两个不相交凸包间的最大距离和最小距离··· 这题就是求凸包的直径 #include <cstdio> #inclu ...

  6. UVA 4728 Squares(凸包+旋转卡壳)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...

  7. UVa 1643 Angle and Squares

    题意: 如图,有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 分析: 直观上来看,当这n个正方形的对角线在一条直线上时,封闭区域的面积最大.( ...

  8. UVA 12113 Overlapping Squares

    题意: 总共有6个2*2的正方形,判断是否能够成所给的形状. 思路: 一个正方形总共有9种摆放方式,对于整个地图来说摆放方式总共有2的9次方种摆放方式.然后将地图用9*5的数组表示,正方形的位置用其8 ...

  9. UVa 1643 Angle and Squares (计算几何)

    题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...

随机推荐

  1. ajax post 时 form数据serialize()

    $.post(UrlAddData, $(".AddForm").serialize(), function (data) { if (data.result) { $.liger ...

  2. wpf datagrid 行双击事件

    Xaml: <DataGrid ItemsSource="{Binding SessionList}" Grid.Row="2" Grid.Column= ...

  3. 2014年辛星完全解读Javascript第六节 对象

    随着面向对象的普及,现在很多语言都在支持面向对象,Javascript也不例外,所谓对象,就是拥有属性和方法的数据.这里的属性其实就是变量,这里的方法,其实就是函数.但是Javascript的面向对象 ...

  4. 【Spring-boot多数据库】Spring-boot JDBC with multiple DataSources sample

    application.properties spring.ds_items.driverClassName=org.postgresql.Driver spring.ds_items.url=jdb ...

  5. Weblogic12c安装与配置详解

    Weblogic是什么Weblogic的安装Weblogic创建域Weblogic管理域Weblogic的应用Weblogic是什么 Weblogic这是我入职以后第一次接触到的词汇,我很陌生,就从我 ...

  6. sql之表连接和group by +组函数的分析

    1.首先我们来先看一个简单的例子: 有[Sales.Orders]订单表和[Sales.Customers]顾客表,表的机构如下 业务要求:筛选  来自“按时打算”国家的用户以及所下的订单数 sele ...

  7. Code for the Homework2

    第二次作业,最近有点忙,一直没写,先发一下,关节角计算有点问题,后面抽时间改 #include<iostream> #include <Eigen/Dense> #includ ...

  8. ExtJs4.2 知识点

    知识点1:修改密码类 参考:点击这里 Ext.apply(Ext.form.VTypes, { password: function (val, field) { if (field.initialP ...

  9. html css 如何将表头固定(转)

    html css 如何将表头固定 position属性取值为fixed时,则元素的位置将不受滚动条的影响,而是直接依据窗口定位,这就是将表头固定的最直接方法,网上其他途径感觉都是在走弯路.但是与此同时 ...

  10. [转载]async & await 的前世今生

    async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...