Squares
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 14689 | Accepted: 5564 |
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.
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
Source
Rocky Mountain 2004
看到题目就想到了hash。
枚举任意两个点作为正方形的边,那么另外两个顶点可以求出来(利用全等三角形)。
注意这样计算的话,每个正方形都被计算了4次,最后结果除以4即可。
我尝试了 对坐标做hash然后用binary_search去查询,和不建hash直接重载<符号然后binary_search,两种算法都可以过。
建立hash的代码:
不建立hash直接重载<符号的代码:
看到题目就想到了hash。
枚举任意两个点作为正方形的边,那么另外两个顶点可以求出来(利用全等三角形)。
注意这样计算的话,每个正方形都被计算了4次,最后结果除以4即可。
我尝试了 对坐标做hash然后用binary_search去查询,和不建hash直接重载<符号然后binary_search,两种算法都可以过。
建立hash的代码:
Source Code
Problem: 2002 User: 775700879
Memory: 716K Time: 2375MS
Language: G++ Result: Accepted
Source Code
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define oo 0x3f3f3f3f using namespace std; vector
v; struct point { int x, y; }; vector
vp; bool operator < (const point & a, const point & b) { if (a.x < b.x) return true; if (a.x > b.x) return false; if (a.y < b.y) return true; if (a.y > b.y) return false; return false; } int result = 0; int main() { int i, j; int n; while (scanf("%d", &n)) { if (n == 0) break; v.clear(); vp.clear(); result = 0; int x, y; int value, value1, value2; for (i = 0; i < n; i++) { scanf("%d%d", &x, &y); value = 40000 * x + y; v.push_back(value); vp.push_back((point){x, y}); } sort(v.begin(), v.end()); sort(vp.begin(), vp.end()); if (n < 4) {cout << 0 << endl; continue;} for (i = 0; i < vp.size(); i++) { for (j = i; j < vp.size(); j++) { if (i == j) continue; int x1, y1, x2, y2; x1 = vp[i].x - (vp[i].y - vp[j].y); y1 = vp[i].y - (vp[j].x - vp[i].x); value1 = x1 * 40000 + y1; x2 = vp[j].x - (vp[i].y - vp[j].y); y2 = vp[j].y - (vp[j].x - vp[i].x); value2 = x2 * 40000 + y2; //if (binary_search(vp.begin(), vp.end(), (point){x1,y1}) && binary_search(vp.begin(), vp.end(), (point){x2,y2})) { if (binary_search(v.begin(), v.end(), value1) && binary_search(v.begin(), v.end(), value2)) { //cout << "square:" << result << endl; //cout << vp[i].x << ":" << vp[i].y << endl; //cout << vp[j].x << ":" << vp[j].y << endl; //cout << x1 << ":" << y1 << endl; //cout << x2 << ":" << y2 << endl; result++; } //if (!binary_search(v.begin(), v.end(), value)) continue; x1 = vp[j].x + (vp[i].y - vp[j].y); y1 = vp[j].y + (vp[j].x - vp[i].x); value1 = x1 * 40000 + y1; //if (!binary_search(v.begin(), v.end(), value)) continue; x2 = vp[i].x + (vp[i].y - vp[j].y); y2 = vp[i].y + (vp[j].x - vp[i].x); value2 = x2 * 40000 + y2; //if (binary_search(vp.begin(), vp.end(), (point){x1,y1}) && binary_search(vp.begin(), vp.end(), (point){x2,y2})) { if (binary_search(v.begin(), v.end(), value1) && binary_search(v.begin(), v.end(), value2)) { //cout << "square:" << result << endl; //cout << vp[i].x << ":" << vp[i].y << endl; //cout << vp[j].x << ":" << vp[j].y << endl; //cout << x1 << ":" << y1 << endl; //cout << x2 << ":" << y2 << endl; result++; } //if (!binary_search(v.begin(), v.end(), value)) continue; } } cout << result / 4 << endl; } return 0; }
不建立hash直接重载<符号的代码:
Source Code
Problem: 2002 User: 775700879
Memory: 720K Time: 3297MS
Language: G++ Result: Accepted
Source Code
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define oo 0x3f3f3f3f using namespace std; vector
v; struct point { int x, y; }; vector
vp; bool operator < (const point & a, const point & b) { if (a.x < b.x) return true; if (a.x > b.x) return false; if (a.y < b.y) return true; if (a.y > b.y) return false; return false; } int result = 0; int main() { int i, j; int n; while (scanf("%d", &n)) { if (n == 0) break; v.clear(); vp.clear(); result = 0; int x, y; int value; for (i = 0; i < n; i++) { scanf("%d%d", &x, &y); value = 40000 * x + y; v.push_back(value); vp.push_back((point){x, y}); } sort(v.begin(), v.end()); sort(vp.begin(), vp.end()); if (n < 4) {cout << 0 << endl; continue;} for (i = 0; i < vp.size(); i++) { for (j = i; j < vp.size(); j++) { if (i == j) continue; int x1, y1, x2, y2; x1 = vp[i].x - (vp[i].y - vp[j].y); y1 = vp[i].y - (vp[j].x - vp[i].x); value = x1 * 40000 + y1; x2 = vp[j].x - (vp[i].y - vp[j].y); y2 = vp[j].y - (vp[j].x - vp[i].x); value = x1 * 40000 + y1; if (binary_search(vp.begin(), vp.end(), (point){x1,y1}) && binary_search(vp.begin(), vp.end(), (point){x2,y2})) { //cout << "square:" << result << endl; //cout << vp[i].x << ":" << vp[i].y << endl; //cout << vp[j].x << ":" << vp[j].y << endl; //cout << x1 << ":" << y1 << endl; //cout << x2 << ":" << y2 << endl; result++; } //if (!binary_search(v.begin(), v.end(), value)) continue; x1 = vp[j].x + (vp[i].y - vp[j].y); y1 = vp[j].y + (vp[j].x - vp[i].x); value = x1 * 40000 + y1; //if (!binary_search(v.begin(), v.end(), value)) continue; x2 = vp[i].x + (vp[i].y - vp[j].y); y2 = vp[i].y + (vp[j].x - vp[i].x); value = x1 * 40000 + y1; if (binary_search(vp.begin(), vp.end(), (point){x1,y1}) && binary_search(vp.begin(), vp.end(), (point){x2,y2})) { //cout << "square:" << result << endl; //cout << vp[i].x << ":" << vp[i].y << endl; //cout << vp[j].x << ":" << vp[j].y << endl; //cout << x1 << ":" << y1 << endl; //cout << x2 << ":" << y2 << endl; result++; } //if (!binary_search(v.begin(), v.end(), value)) continue; } } cout << result / 4 << endl; } return 0; }