题意:

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

分析:

这种题一看就有思路,最开始的想法就是枚举正方形的位置,需要二重循环,枚举边长一重循环,判断是否为正方形又需要一重循环,复杂度为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后面,哪个数代表行,哪个数代表列。

  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. const int maxn = ;
  5. bool G[][maxn][maxn];
  6. int a[][maxn][maxn], cnt[maxn];
  7.  
  8. int main()
  9. {
  10. //freopen("in.txt", "r", stdin);
  11. int n, m, kase = ;
  12. while(scanf("%d", &n) == && n)
  13. {
  14. memset(G, false, sizeof(G));
  15. memset(a, , sizeof(a));
  16. memset(cnt, , sizeof(cnt));
  17. scanf("%d", &m);
  18. getchar();
  19. for(int k = ; k < m; ++k)
  20. {
  21. char c;
  22. int i, j;
  23. scanf("%c %d %d", &c, &i, &j);
  24. getchar();
  25. if(c == 'H') G[][i][j+] = true;
  26. else G[][j+][i] = true;
  27. }
  28. for(int i = ; i <= n; ++i)
  29. for(int j = ; j <= n; ++j)
  30. {
  31. a[][i][j] = a[][i][j-] + G[][i][j];
  32. a[][i][j] = a[][i-][j] + G[][i][j];
  33. }
  34.  
  35. for(int i = ; i < n; ++i)
  36. for(int j = ; j < n; ++j) //枚举正方形的左上角
  37. for(int l = ; i+l<=n && j+l<=n; ++l) //枚举正方形的边长
  38. if(a[][i][j+l]-a[][i][j] == l && a[][i+l][j+l]-a[][i+l][j] == l
  39. && a[][i+l][j]-a[][i][j] == l && a[][i+l][j+l]-a[][i][j+l] == l)
  40. cnt[l]++;
  41.  
  42. if(kase) printf("\n**********************************\n\n");
  43. printf("Problem #%d\n\n", ++kase);
  44. bool flag = false;
  45. for(int i = ; i <= n; ++i) if(cnt[i])
  46. {
  47. printf("%d square (s) of size %d\n", cnt[i], i);
  48. flag = true;
  49. }
  50. if(!flag) puts("No completed squares can be found.");
  51. }
  52.  
  53. return ;
  54. }

代码君

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. phpcms常用接口调用方法

    常用函数 , 打开include/global.func.php,下面存放一些公共函数 view plaincopy to clipboardprint?function str_charset($i ...

  2. CorelDRAW 二维码插件

    随着智能手机的流行,二维码在各个领域大量应用,这个插件在补CorelDRAW这方面的不足: 这个插件是 cpg 格式,安装请看这篇博客:http://www.cnblogs.com/o594cql/p ...

  3. jQuery基础与实例

    一.简介 1.什么是jQuery jQuery是一个轻量级.快速简洁的javaScript库,能让我们方便快捷的选择元素操作元素属性. 2.下载地址 3.jQuery使用方式 $("div& ...

  4. max_size, capacity and size 的区别

    The max_size() function returns the maximum number of elements that the container can hold. The max_ ...

  5. 针对PIL中ImageDraw.py报错的解决方案

    linux mint 13开始就发现这个问题了,一直不知道怎么解决,今天突然发现了解决方案,来分享给大家 下面是修改对比,自己根据修改,这个是系统文件,需要root权限,路径/usr/lib/pyth ...

  6. 长安铃木经销商爬取(解析xml、post提交、python中使用js代码)

    1.通过火狐浏览器,查找大长安铃木官网中关于经销商的信息主要在两个网页中 http://www.changansuzuki.com/khfw/xml/pro.xml  地域信息 http://www. ...

  7. Oracle表连接

    一个普通的语句select * from t1, t2 where t1.id = t2.id and t1.name = 'a'; 这个语句在什么情况下最高效? 表连接分类: 1. 嵌套循环连接(N ...

  8. Hibernate从入门到精通(八)一对多单向关联映射

    上次的博文Hibernate从入门到精通(七)多对一单向关联映射我们主要讲解了一下多对一单向关联映射,这次我们继续讲解一下一对多单向映射. 一对多单向关联映射 在讲解一对多单向关联之前,按照我们的惯例 ...

  9. 类的本质、description方法、SEL、NSLog输出增强

    一.类的本质 1.类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class *Class; 类名就代表着类对象 ...

  10. 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.