1. 程式人生 > >POJ 2002 二分 計算幾何

POJ 2002 二分 計算幾何

algo log per esp nod truct clu continue spa

根據正方形對角的兩頂點求另外兩個頂點公式:

x2 = (x1+x3-y3+y1)/2; y2 = (x3-x1+y1+y3)/2;

x4= (x1+x3+y3-y1)/2; y4 = (-x3+x1+y1+y3)/2;

#include<cstdio>  
#include<cstring>  
#include<algorithm>  
using namespace std;  
const int maxn=1000+5;  
struct Node  
{  
    int x,y;  
    bool operator<(const
Node& rhs)const { return x<rhs.x || (x==rhs.x && y<rhs.y); } }nodes[maxn]; int main() { int n; while(scanf("%d",&n)==1 && n) { int ans=0;//正方形個數 for(int i=0;i<n;++i) scanf("%d%d
",&nodes[i].x,&nodes[i].y); sort(nodes,nodes+n); for(int i=0;i<n;++i) for(int j=i+1;j<n;++j) { Node tmp;//tmp作為正方形的第3或4個點 tmp.x=nodes[i].x+nodes[i].y-nodes[j].y; tmp.y=nodes[i].y+nodes[j].x-nodes[i].x;
if(!binary_search(nodes,nodes+n,tmp)) continue; tmp.x=nodes[j].x+nodes[i].y-nodes[j].y; tmp.y=nodes[j].y+nodes[j].x-nodes[i].x; if(!binary_search(nodes,nodes+n,tmp)) continue; ++ans; } printf("%d\n",ans/2); } return 0; }

POJ 2002 二分 計算幾何