poj2002 数正方形 hash

直接上渣代码了

#include 
using namespace std;
struct gtype {
       int x,y,next;
}g[3000];
int first[1000100],tot,n,x[3010],y[3010],ans,h,tx,ty;
int hash(int x,int y) {
    return (40000*x+y+800040000)%999991+1;
}
bool find(int x,int y) {
     int h = hash(x,y);
     bool f = false;
     for (int t=first[h];t!=-1;t=g[t].next)
         if (g[t].x==x && g[t].y==y) {     
            f = true;
            break;
         } 
     return f;
}
int main() {
    while (~scanf("%d",&n)) {
          tot = 0;
          memset(first,-1,sizeof(first));
          for (int i=1;i<=n;i++) {
              scanf("%d%d",&x[i],&y[i]);
              h = hash(x[i],y[i]);
              tot++;
              g[tot].x = x[i];
              g[tot].y = y[i];
              g[tot].next = first[h];
              first[h] = tot;
          }
          ans = 0;
          for (int i=1;i<=n;i++)
              for (int j=i+1;j<=n;j++) {
                  if (i==j) continue;
                  tx = x[i]-x[j];
                  ty = y[i]-y[j];
                  if (find(x[i]+ty,y[i]-tx) && find(x[j]+ty,y[j]-tx)) ans++;
                  if (find(x[i]-ty,y[i]+tx) && find(x[j]-ty,y[j]+tx)) ans++;
              }
          printf("%d\n",ans/4);
    }
    return 0;
}


你可能感兴趣的:(POJ)