• POJ 2002 Squares [hash]


    Squares
    Time Limit: 3500MS   Memory Limit: 65536K
    Total Submissions: 16631   Accepted: 6328

    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

    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

    题解:枚举两个点,算出另外两点坐标,看是否在给的点集里。

    具体实现用hash,hash碰撞了就放在链表中,然后在链表里查找~(天猫所说的  pascal拉链哈希,,,

    就写了个哈希函数然后对函数值指针拉链出来哈希)

      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<map>
      6 #define ll long long
      7 #define N 1005
      8 #define mod 2007
      9 
     10 using namespace std;
     11 
     12 int n;
     13 int x[2*N];
     14 int y[2*N];
     15 //map<pair<int,int>,int>mp;
     16 int ans;
     17 int head[2*N];
     18 int next[2*N];
     19 int m;
     20 
     21 void insert(int i)
     22 {
     23     int key=(x[i]*x[i]+y[i]*y[i])%mod;
     24     next[m]=head[key];
     25     x[m]=x[i];
     26     y[m]=y[i];
     27     head[key]=m++;
     28 }
     29 
     30 void ini()
     31 {
     32     ans=0;
     33     m=N;
     34     memset(head,-1,sizeof(head));
     35     //mp.clear();
     36     int i;
     37     for(i=1;i<=n;i++){
     38         scanf("%d%d",&x[i],&y[i]);
     39         insert(i);
     40         //mp[ make_pair(x[i],y[i]) ]=i;
     41     }
     42 }
     43 
     44 int find(int xx,int yy)
     45 {
     46     int key=(xx*xx+yy*yy)%mod;
     47     int i;
     48     for(i=head[key];i!=-1;i=next[i]){
     49         if(x[i]==xx && y[i]==yy){
     50             return 1;
     51         }
     52     }
     53     return 0;
     54 }
     55 
     56 int ok(int i,int j)
     57 {
     58     int mx,my;
     59     int x3,x4,y3,y4;
     60     int he,cha;
     61     mx=x[i]+x[j];
     62     my=y[i]+y[j];
     63     he=mx+my;cha=my-mx;
     64     if(he%2!=0 || cha%2!=0) return 0;
     65     he/=2;cha/=2;
     66     x3=he-y[i];
     67    // x3=mx+my-y[i];
     68     y3=cha+x[i];
     69  //   y3=my-(mx-x[i]);
     70     if(find(x3,y3)==0) return 0;
     71     x4=y[i]-cha;
     72    // x4=mx-(my-y[i]);
     73    // y4=my+(mx-x[i]);
     74     y4=he-x[i];
     75     if(find(x4,y4)==0) return 0;
     76 
     77     return 1;
     78 }
     79 
     80 void solve()
     81 {
     82     int i,j;
     83     for(i=1;i<n;i++){
     84         for(j=i+1;j<=n;j++){
     85             if(ok(i,j)==1){
     86                 ans++;
     87             }
     88         }
     89     }
     90 }
     91 
     92 void out()
     93 {
     94     ans/=2;
     95     printf("%d
    ",ans);
     96 }
     97 
     98 int main()
     99 {
    100     //freopen("data.in","r",stdin);
    101    // scanf("%d",&T);
    102     //while(T--){
    103     while(scanf("%d",&n)!=EOF){
    104         if(n==0) break;
    105         ini();
    106         solve();
    107         out();
    108     }
    109     return 0;
    110 }
  • 相关阅读:
    第30节:Java基础-内部类
    第30节:Java基础-内部类
    gridview无数据源实现更新数据库(即断开更新数据库)
    net8:文本文件的创建及其读写
    智勇之家网页颜色代码自动生成
    对象数据源objectdatasource的使用,类的编写实现查询增删改的方法
    清清月儿.net学习技术资料网站
    可移植的数据库
    net3:文件上传与图片显示以及HiddenField隐藏字段值的使用
    C#.net磁盘管理以及文件操作
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4160159.html
Copyright © 2020-2023  润新知