Regular polygon
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 857 Accepted Submission(s): 312
Problem Description
On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make.
Input
The input file consists of several test cases. Each case the first line is a numbers N (N <= 500). The next N lines ,each line contain two number Xi and Yi(-100 <= xi,yi <= 100), means the points’ position.(the data assures no two points share the same position.)
Output
For each case, output a number means how many different regular polygon these points can make.
Sample Input
4 0 0 0 1 1 0 1 1 6 0 0 0 1 1 0 1 1 2 0 2 1
Sample Output
1 2
题意:
给出n点,坐标都是整数(没有好好审题,后来才发现)
所以组成的多边形都是正方形(画一个图就能懂了)
题解:
枚举对角线,求另外两个点,把点的范围处理一下,就可以用二维数组来记录是否有这个点了
官方题解用的map + pair ,我的第一份代码也是的,因为查找超时, 需要使用map中的count函数才不会超时
本题主要就是在查找点
可以用二分查找点,也可以hash查找点,也可以用上叙方法
stl方法
#include<map>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
};
node p[1000];
pair<int,int> k,k1,k2;
map<pair<int,int>,bool>mp;
int main()
{
int n;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
mp.clear();
for(int i=1;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
p[i].x*=2;
p[i].y*=2;
k = make_pair(p[i].x,p[i].y);
mp[k]=1;
}
int ans=0;
node t1,t2;
int x1,x2;
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
t1.x=(p[i].x+p[j].x+p[i].y-p[j].y)/2;
t2.x=p[i].x+p[j].x-t1.x;
t1.y=(p[i].y+p[j].y+p[j].x-p[i].x)/2;
t2.y=p[i].y+p[j].y-t1.y;
k1=make_pair(t1.x,t1.y);
k2=make_pair(t2.x,t2.y);
x1=mp.count(k1);
x2=mp.count(k2);
if(x1&&x2)
ans++;
}
}
printf("%d\n",ans/2);
}
return 0;
}
visit二维坐标记录点的代码
#include<map>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
};
node p[1000];
int visit[1000][1000];
int main()
{
int n,a,b;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
memset(visit,0,sizeof(visit));
for(int i=1;i<=n;i++){
scanf("%d%d",&a,&b);
p[i].x=(a+200)*2,p[i].y=(b+200)*2;
visit[p[i].x][p[i].y]=1;
}
int ans=0;
node t1,t2;
int x1,x2;
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
t1.x=(p[i].x+p[j].x+p[i].y-p[j].y)/2;
t2.x=p[i].x+p[j].x-t1.x;
t1.y=(p[i].y+p[j].y+p[j].x-p[i].x)/2;
t2.y=p[i].y+p[j].y-t1.y;
x1=visit[t1.x][t1.y];
x2=visit[t2.x][t2.y];
if(x1&&x2)
ans++;
}
}
printf("%d\n",ans/2);
}
return 0;
}
hash查找点的代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1010;
const int H = 10007;
int ptx[N], pty[N];
struct Node
{
int x;
int y;
int next;
};
Node node[N];
int cur;
int n;
long ans;
int hashTable[H];
void initHash()
{
for (int i = 0; i < H; ++i) hashTable[i] = -1;
cur = 0;
ans = 0;
}
void insertHash(int x, int y)
{
int h = (x * x + y * y) % H;
node[cur].x = x;
node[cur].y = y;
node[cur].next = hashTable[h];
hashTable[h] = cur;
++cur;
}
bool searchHash(int x, int y)
{
int h = (x * x + y * y) % H;
int next;
next = hashTable[h];
while (next != -1)
{
if (x == node[next].x && y == node[next].y) return true;
next = node[next].next;
}
return false;
}
int main()
{
while (scanf("%d", &n) != EOF && n)
{
initHash();
for (int i = 0; i < n; ++i)
{
scanf("%d%d", &ptx[i], &pty[i]);
insertHash(ptx[i], pty[i]);
}
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
int x1 = ptx[i] - (pty[i] - pty[j]);
int y1 = pty[i] + (ptx[i] - ptx[j]);
int x2 = ptx[j] - (pty[i] - pty[j]);
int y2 = pty[j] + (ptx[i] - ptx[j]);
if (searchHash(x1, y1) && searchHash(x2, y2)) ++ans;
}
}
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
int x1 = ptx[i] + (pty[i] - pty[j]);
int y1 = pty[i] - (ptx[i] - ptx[j]);
int x2 = ptx[j] + (pty[i] - pty[j]);
int y2 = pty[j] - (ptx[i] - ptx[j]);
if (searchHash(x1, y1) && searchHash(x2, y2)) ++ans;
}
}
ans >>= 2;
printf("%ld\n", ans);
}
return 0;
}
二分查找点的代码
bool _find(double a,double b) //二分查找
{
int l = 0,r = n-1;
while(l<=r){
int mid = l + r >> 1;
if(abs(no[mid].a-a)<eps && abs(no[mid].b-b)<eps) return 1;
if( (no[mid].a < a && abs(no[mid].a - a) > eps) || (abs(no[mid].a - a) < eps && (no[mid].b < b && abs(no[mid].b - b) > eps))){
l = mid + 1;
}else r = mid - 1;
}
return 0;
}