poj 2002 Squares
2015-08-14 11:17
399 查看
Description
A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with
the latter property, however, as a regular octagon also has this property.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x
and y coordinates.
Input
The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each
point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.
Output
For each test case, print on a line the number of squares one can form from the given stars.
Sample Input
Sample Output
A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with
the latter property, however, as a regular octagon also has this property.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x
and y coordinates.
Input
The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each
point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.
Output
For each test case, print on a line the number of squares one can form from the given stars.
Sample Input
4 1 0 0 1 1 1 0 0 9 0 0 1 0 2 0 0 2 1 2 2 2 0 1 1 1 2 1 4 -2 5 3 7 0 0 5 2 0
Sample Output
1 6 1
哈希表做法,程序略渣 #include <iostream> #include <cstring> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <cmath> #define LL long long #define INF 0x3f3f3f3f #define Mod 1000007 using namespace std; struct node { int x,y; int next; } ls[1010]; int top; int Hash[Mod]; int x[1010],y[1010]; void Add(int x,int y) { int a = (x * x + y * y) % Mod; ls[top].x = x; ls[top].y = y; ls[top].next = Hash[a]; Hash[a] = top++; } bool Search(int x,int y) { int a = (x * x + y * y) % Mod; int next = Hash[a]; while(next != -1 ) { if(ls[next].x == x && ls[next].y == y) return true; next = ls[next].next; } return false; } int main() { // freopen("in.txt","r",stdin); int n; while(cin>>n && n) { top = 0; memset(Hash,-1,sizeof(Hash)); for(int i=0; i<n; i++) { cin>>x[i]>>y[i]; Add(x[i],y[i]); } LL ant = 0; for(int i=0; i<n-1; i++) { for(int j=i+1; j<n; j++) { int xx = x[i] - x[j]; int yy = y[i] - y[j]; int x3 = x[i] + yy; int y3 = y[i] - xx; int x4 = x[j] + yy; int y4 = y[j] - xx; if(Search(x3,y3) && Search(x4,y4)) ant++; x3 = x[i] - yy; y3 = y[i] + xx; x4 = x[j] - yy; y4 = y[j] + xx; if(Search(x3,y3) && Search(x4,y4)) ant++; } } cout<<ant/4<<endl; } return 0; } //二分查找 <pre name="code" class="html">#include <iostream> #include <cstring> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <cmath> #define LL long long #define INF 0x3f3f3f3f #define Mod 1000007 using namespace std; struct node { int x,y; } ls[1010]; bool cmp(node a,node b) { if(a.x == b.x) return a.y < b.y; return a.x < b.x; } bool Search(int low,int high,int x,int y) { int i = low,j = high; while(i <= j) { int mid = (i + j) / 2; if(ls[mid].x == x && ls[mid].y == y) return true; if(ls[mid].x < x) i = mid + 1; else if(ls[mid].x > x) j = mid - 1; else if(ls[mid].x == x) { if(ls[mid].y < y) i = mid + 1; else if(ls[mid].y > y) j = mid - 1; } } return false; } int main() { std::ios::sync_with_stdio(false); int n; while(cin>>n && n) { for(int i=0; i<n; i++) { cin>>ls[i].x>>ls[i].y; } sort(ls,ls+n,cmp); int ant = 0; for(int i=0; i<n-1; i++) { for(int j=i+1; j<n; j++) { int xx = ls[i].x - ls[j].x; int yy = ls[i].y - ls[j].y; int x3 = ls[i].x - yy; int y3 = ls[i].y + xx; int x4 = ls[j].x - yy; int y4 = ls[j].y + xx; if(Search(0,n-1,x3,y3) && Search(0,n-1,x4,y4)) ant++; x3 = ls[i].x + yy; y3 = ls[i].y - xx; x4 = ls[j].x + yy; y4 = ls[j].y - xx; if(Search(0,n-1,x3,y3) && Search(0,n-1,x4,y4)) ant++; } } cout<<ant/4<<endl; } return 0; }
相关文章推荐
- HDU 5387 Clock (简单计算)
- 线段树 + 扫描线加深详解
- Android应用:Service开发实例(二)之Service的两种创建方法和其生命周期
- iOS中利用NSURLSession进行文件断点下载
- java动态缓存成长小纪(二)——缓存算法的实现:LRU、LFU、FIFO
- Android网络编程之如何去掉WebView【浏览器】中的进度条和网址
- jquery attr prop checkbox已有checked=checked但不显示勾选问题
- ecshop第一讲之安装
- Linux基础学习笔记-第四课:NANO、开关机
- 浅谈使用java解析和生成JSON
- 关于编码ansi、GB2312、unicode与utf-8的区别(带源码下载)
- hdoj.5090 Game with Pearls【二分图匹配】 2015/08/14
- Class^=,Class*= ,Class$=含义
- ZigBee网络体系结构(图文)
- 新浪微博SDK抛出异常-[__NSDictionaryM weibosdk_WBSDKJSONString]: unrecognized selector sent to instance 0x7fb
- hdu 1047 Integer Inquiry(大数)
- [转]网游服务端开发入门知识
- 枚举应用五
- Java中的Scanner类和String类
- 北国城,徒留旧梦