Regular polygon
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 170 Accepted Submission(s): 70
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
Source
Recommend
题意:
题意是给了n个点,问着这个图中有多少个正多边形
题解:
因为给出的点都是整数点,所以满足条件的正多边形只有正方形。
先找到两个点,然后枚举其他点,看是否能够找到其他两个满足条件的点。
这样枚举每条边,一个正方形被算了四遍,所以最后的结果要除以4。
一条边两边都可能存在正方形。所以算两遍。
设两个点(x1 , y1)和(x2, y2)那么查询是否存在(x1 + y1-y2, y1+x2-x1),(x2+y1-y2, y2+x2-x1)
与(x1-(y1-y2), y1-(x2-x1)), (x2-(y1-y2), y2-(x2-x1))。假设(x1,y1)在(x2,y2)上面。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N= 500;
int vis[N][N];
int a[505][2];
int caculatorCount(int i, int j) {
int sum = 0;
int x = a[j][0]-a[i][0];
int y = a[i][1]-a[j][1];
if(a[i][0]+y >= 0 && a[i][1]+x >= 0 && a[j][0]+y >= 0 && a[j][1]+x >= 0 &&
vis[a[i][0]+y][a[i][1]+x] && vis[a[j][0]+y][a[j][1]+x])
sum++;
if(a[i][0]-y >=0 && a[i][1]-x >= 0 && a[j][0]-y >= 0 && a[j][1]-x >= 0 &&
vis[a[i][0]-y][a[i][1]-x] && vis[a[j][0]-y][a[j][1]-x])
sum++;
return sum;
}
int main() {
int n, Count;
while(scanf("%d", &n) != EOF) {
Count = 0;
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++) {
scanf("%d%d", &a[i][0], &a[i][1]);
a[i][0] += 200;
a[i][1] += 200;
vis[a[i][0]][a[i][1]] = 1;
}
for(int i = 1; i < n; i++)
for(int j = i+1; j <= n; j++) {
Count += caculatorCount(i, j);
}
printf("%d\n", Count/4);
}
}