Poj2002 Squares
题意描述:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少。相同的四个点,不同顺序构成的正方形视为同一正方形。
思路变迁:
1、最简单的方法,直接暴力搜索,即依次取四个顶点,根据其坐标判断是否能组成正方形。组成正方形的条件是四个顶点可组成的六条边里面,有四条相等,剩下两条相等。当然由于其时间复杂度为O(n^4),所以提交结果为TLE
2、考虑降低时间复杂度。如任取三个顶点,根据组成正方形的条件计算得到第四个顶点,判断其是否在点的集合内,其复杂度为O(n^3)。或者任取两个顶点,根据组成正方形的条件计算出另外两个顶点,再判断其是否在点的集合内,其复杂度为O(n^2)。
3、不论是任取两个顶点还是任取三个顶点,其关键一步都是判断计算得到的新的点,是否在点的集合内,基本方法有对点排序再二分查找,或者是使用hash表。我的设计是考虑点所处象限的二维hash表。根据这道题的提交结果来看,所用时间上,堆排序优于快排
4、对于任选两个顶点时,可能存在三种不同的正方形。但是在对所有顶点排序后,如若按序来去顶点,则可发现我们只需始终计算一个方向的即可,且计算结果中同一个正方形重复了两次,所以最终结果除以二。如若不排序直接任取两个的顶点的话,始终按一个方向计算,可以发现每个正方形的四条边,每一条边都会贡献一次得到的正方形的个数,所以最终结果需要除以四
5、在使用hash表后时间复杂度达到最优,即O(nlgn) + hash表平均冲突*n(n-1)/2,所以本问题的进一步优化,取决于怎么更大程度的降低hash表的冲突?考虑位运算,来存储所有的点,但需要空间太大,因为共有40000*40000个点
暴力搜索代码
#include<stdio.h>
#define SIZE 1005
typedef struct POINT {
int x;
int y;
} point;
int isSquare(int a, int b, int c, int d);
point p[SIZE];
int main() {
int n, result;
int i, j, k, x;
scanf("%d", &n);
while(n != ) {
result = ;
for(i=; i<=n; i++)
scanf("%d %d", &p[i].x, &p[i].y);
for(i=; i<=n; i++) {
for(j=i+; j<=n; j++) {
for(k=j+; k<=n; k++) {
for(x=k+; x<=n; x++) {
if(isSquare(i, j, k, x))
result++;
}
}
}
}
printf("%d\n", result);
scanf("%d", &n);
}
} int isSquare(int a, int b, int c, int d) {
int s[];
int num1 = , num2 = ;
int side1 = -, side2 = -;
int i;
s[] = (p[a].x - p[b].x)*(p[a].x - p[b].x) + (p[a].y - p[b].y)*(p[a].y - p[b].y);
s[] = (p[a].x - p[c].x)*(p[a].x - p[c].x) + (p[a].y - p[c].y)*(p[a].y - p[c].y);
s[] = (p[a].x - p[d].x)*(p[a].x - p[d].x) + (p[a].y - p[d].y)*(p[a].y - p[d].y);
s[] = (p[b].x - p[c].x)*(p[b].x - p[c].x) + (p[b].y - p[c].y)*(p[b].y - p[c].y);
s[] = (p[b].x - p[d].x)*(p[b].x - p[d].x) + (p[b].y - p[d].y)*(p[b].y - p[d].y);
s[] = (p[c].x - p[d].x)*(p[c].x - p[d].x) + (p[c].y - p[d].y)*(p[c].y - p[d].y);
for(i=; i<=; i++) {
if(i == ) {
side1 = s[i];
}
else if(s[i] != side1) {
if(side2 == -)
side2 = s[i];
else if(s[i] == side2)
num2++;
else
break;
}
else if(s[i] == side1)
num1++;
}
if((num1== && num2==) || (num1== && num2 == ))
return ;
else
return ;
}
使用堆排序+二分查找代码
#include<stdio.h>
#define SIZE 1005
typedef struct POiNT {
int x;
int y;
} point;
int cmp(point pa, point pb);
void heapSort(int n);
int binarySearch(int low, int high, point key);
void shiftDown(int i, int n);
void swap(int i, int j);
point p[SIZE];
int main() {
int n, result;
int i, j, k, x;
point pa, pb;
scanf("%d", &n);
while(n != ) {
result = ;
for(i=; i<=n; i++)
scanf("%d %d", &p[i].x, &p[i].y);
heapSort(n);
for(i=; i<=n; i++)
printf("%d %d\n", p[i].x, p[i].y); for(i=; i<=n; i++) {
for(j=i+; j<=n; j++) {
pa.x = p[i].x - (p[j].y - p[i].y);
pa.y = p[i].y + (p[j].x - p[i].x);
pb.x = pa.x + (p[j].x - p[i].x);
pb.y = pa.y + (p[j].y - p[i].y);
if(binarySearch(, n, pa) && binarySearch(, n, pb))
result++;
}
}
printf("%d\n", result/);
scanf("%d", &n);
}
}
int binarySearch(int low, int high, point key) {
int mid = (low + high)/;
if(low > high)
return ;
if(cmp(p[mid], key) == )
return ;
else if(cmp(p[mid], key) == )
return binarySearch(low, mid-, key);
else
return binarySearch(mid+, high, key);
}
void heapSort(int n) {
int i = n/;
for(; i>; --i)
shiftDown(i, n);
for(i=n; i>;) {
swap(, i);
shiftDown(, --i);
}
}
void shiftDown(int i, int n) {
int j = *i;
for(; j<=n; j=*i) {
if(j < n && cmp(p[j], p[j+]) == -)
j = j + ;
if(cmp(p[i], p[j]) == -) {
swap(i, j);
i = j;
} else
break;
}
}
void swap(int i, int j) {
point temp = p[i];
p[i] = p[j];
p[j] = temp;
}
int cmp(point pa, point pb) {
if(pa.x > pb.x)
return ;
else if(pa.x == pb.x && pa.y > pb.y)
return ;
else if(pa.x == pb.x && pa.y == pb.y)
return ;
else
return -;
}
考虑象限的hash代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 1013
typedef struct POiNT {
int x;
int y;
} point;
struct hashNode {
point poi;
struct hashNode *next;
};
int cmp(const void *a, const void *b );
void add(point key);
int find(point);
point p[SIZE];
struct hashNode hashTable[][SIZE];
int main() {
int n, result;
int i, j, k, x;
point pa, pb;
scanf("%d", &n);
while(n != ) {
result = ;
memset(hashTable, , sizeof(hashTable));
for(i=; i<=n; i++) {
scanf("%d %d", &p[i].x, &p[i].y);
add(p[i]);
}
qsort(p+, n, sizeof(p[]), cmp);
for(i=; i<=n; i++) {
for(j=i+; j<=n; j++) {
pa.x = p[i].x - (p[j].y - p[i].y);
pa.y = p[i].y + (p[j].x - p[i].x);
pb.x = pa.x + (p[j].x - p[i].x);
pb.y = pa.y + (p[j].y - p[i].y);
if(find(pa) && find(pb))
result++;
}
}
printf("%d\n", result/);
scanf("%d", &n);
}
}
void add(point key) {
int t, num;
struct hashNode *newNode;
if(key.x >= && key.y >= )
t = ;
else if(key.x >= && key.y < )
t = ;
else if(key.x < && key.y < )
t = ;
else
t = ;
num = (key.x*key.x + key.y*key.y)%SIZE;
newNode = (struct hashNode*)malloc(sizeof(struct hashNode));
newNode->poi = key;
newNode->next = hashTable[t][num].next;
hashTable[t][num].next = newNode;
}
int find(point key) {
int t, num;
struct hashNode *node;
if(key.x >= && key.y >= )
t = ;
else if(key.x >= && key.y < )
t = ;
else if(key.x < && key.y < )
t = ;
else
t = ;
num = (key.x*key.x + key.y*key.y)%SIZE;
node = hashTable[t][num].next;
while(node) {
if(node->poi.x == key.x && node->poi.y == key.y)
return ;
node = node->next;
}
return ;
}
int cmp(const void *a, const void *b )
{
point c = *(point*)a;
point d = *(point*)b; if(c.x == d.x)
return c.y - d.y;
else
return c.x - d.x;
}
Poj2002 Squares的更多相关文章
- poj2002 Squares(hash+折半枚举)
Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-d ...
- POJ-2002 Squares,哈希模板+数学公式!
Squares 题意:二维坐标轴给出n个点求有多少个正方形. 要是平时做比赛的话毫无疑问会 ...
- [POJ2002]Squares(计算几何,二分)
题目链接:http://poj.org/problem?id=2002 给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化. 有公 ...
- POJ2002 Squares(枚举)
题目链接. 分析: 普遍的做法是:先枚举两个点,通过数学公式得到另外2个点,使得这四个点能够成正方形.然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形. 但这种做法会使同一个正方形 ...
- poj2002 hash+邻接表优化Squares
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 17487 Accepted: 6643 Descript ...
- [poj2002]Squares_hash
Squares poj-2002 题目大意:在笛卡尔坐标系中给出n个点,求这些点可以构成多少个正方形. 注释:$1\le n\le 10^3$,$-2\cdot 10^3\le x , y\le 2\ ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- 卡通图像变形算法(Moving Least Squares)附源码
本文介绍一种利用移动最小二乘法来实现图像变形的方法,该方法由用户指定图像中的控制点,并通过拖拽控制点来驱动图像变形.假设p为原图像中控制点的位置,q为拖拽后控制点的位置,我们利用移动最小二乘法来为原图 ...
- Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
随机推荐
- HeadFirst设计模式之模板方法模式
一. 1.The Template Method defines the steps of an algorithm and allows subclasses to provide the impl ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-003-把用户数据存在数据库
一. 1.It’s quite common for user data to be stored in a relational database, accessed via JDBC . To c ...
- 使用intellij idea搭建MAVEN+springmvc+mybatis框架
原文:使用intellij idea搭建MAVEN+springmvc+mybatis框架 1.首先使用idea创建一个maven项目 2.接着配置pom.xml,以下为我的配置 <projec ...
- P94、面试题12:打印1到最大的n位数
题目:输入数字n,按顺序打印出从1最大的n位十进制数.比如输入3,则打印出1,2,3一直到最大的3位数999. 思路:先把字符串中的每一个数字都初始化为‘0’,然后每一次为字符串表示的数字加1,再打印 ...
- 关于fedora下jdk的安装
http://zhumeng8337797.blog.163.com/blog/static/1007689142012472620637/ alternative命令 http://blog.csd ...
- PHP实现浏览历史记录
http://www.3a88.com/service/206.html http://www.1360.cc/ZhanChangJiaoCheng/6831.html http://www.osch ...
- itoa函数的实现(不同进制)
2013-07-08 17:12:30 itoa函数相对于atoi函数,比较简单,还是要注意考虑的全面. 小结: 一下几点需要考虑: 对负数,要加上负号: 考虑不同进制,根据要求进行处理:对不同的进制 ...
- C# 字符串加密解密方法
这个是加密的算法的命名空间,使用加密算法前要引用该程序集 System.Security.Cryptography using System;using System.Data;using Syst ...
- 【HDOJ】4343 Interval query
最大不相交集合的数量.思路是dp[i][j]表示已经有i个不相交集合下一个不相交集合的最右边界.离散化后,通过贪心解. /* 4343 */ #include <iostream> #in ...
- java中synchronized的用法详解
记下来,很重要. Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchron ...