poj- 2002-Squares-哈希|除法散列法
来源:互联网 发布:网络约车平台 计划书 编辑:程序博客网 时间:2024/06/06 08:51
题意:
给你n 个点的坐标,让你去算一下,能够形成多少个正方形
思路:
1.枚举两个点,然后推出那两个的坐标,这样会有重复的,最后需要除以4
已知: (x1,y1) (x2,y2)
则: x3=x1+(y1-y2) y3= y1-(x1-x2)
x4=x2+(y1-y2) y4= y2-(x1-x2)
或
x3=x1-(y1-y2) y3= y1+(x1-x2)
x4=x2-(y1-y2) y4= y2+(x1-x2)
数学公式,也是看的网上的,自己没去推,同学们不要学我啊2. 点的寻找要用哈希的方法,链地址法还是啥搞不懂,我用的是数组模拟链表,除法散列法
求 key 的值的时候用的平方求于法
#include<stdio.h>#include<string.h>#include<stdlib.h>#define mod 100007int tmp;struct node{ int x, y; int next;}ls[1010],a[1010];int head[mod];void add(int x,int y){ int key = (x*x + y*y)%mod; if(head[key]==-1) { ls[tmp].x = x; ls[tmp].y = y; ls[tmp].next = -1; head[key] = tmp++; } else { int c = head[key]; while(1) { if(ls[c].x == x && ls[c].y == y) { return; } if(ls[c].next == -1) break; c = ls[c].next; } ls[tmp].x = x; ls[tmp].y = y; ls[tmp].next = -1; ls[c].next = tmp++; }}int sear(int x, int y){ int ad = (x*x +y*y)%mod; int c= head[ad]; while(c!=-1) { if(ls[c].x == x && ls[c].y == y) return 1; c = ls[c].next; } return 0;}int main(){ int n; while(~scanf("%d",&n)&&n) { tmp = 1; memset(head,-1,sizeof(head)); int i, j; for(i = 0; i < n; i++) { int x, y; scanf("%d%d",&x,&y); a[i].x = x; a[i].y = y; add(x,y); } int x3, y3, x4, y4; int cnt = 0; for(i = 0; i < n-1; i++) { for(j = i+1; j < n; j++) { x3 = a[i].x - (a[i].y-a[j].y); y3 = a[i].y + (a[i].x-a[j].x); x4 = a[j].x - (a[i].y - a[j].y); y4 = a[j].y + (a[i].x -a[j].x); if(sear(x3,y3)&&sear(x4,y4)) cnt++; x3 = a[i].x + (a[i].y-a[j].y); y3 = a[i].y - (a[i].x-a[j].x); x4 = a[j].x + (a[i].y - a[j].y); y4 = a[j].y - (a[i].x -a[j].x); if(sear(x3,y3)&&sear(x4,y4)) cnt++; } } printf("%d\n",cnt/4); } return 0;}
2
map
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<map>#include<queue>#include<climits>#include<list>#define MULT 20000using namespace std;struct node{ int x,y;}st[1200];int main(){ int n; while(scanf("%d",&n),n!=0) { map<pair<int ,int>,int>hash_list; int i; int j; for(i=0;i<n;++i) { scanf("%d%d",&st[i].x,&st[i].y); hash_list[pair<int,int>(st[i].x,st[i].y)]; } int sum=0; for(i=0;i<n;++i) { for(j=i+1;j<n;++j) { int x,y,xx,yy; x=(st[i].x+st[j].x+st[i].y-st[j].y); y=(st[i].y+st[j].y+st[j].x-st[i].x); xx=(st[i].x+st[j].x-st[i].y+st[j].y); yy=(st[i].y+st[j].y-st[j].x+st[i].x); if(x&1||y&1||xx&1||yy&1) { continue; } x/=2; y/=2; xx/=2; yy/=2; if(hash_list.count(pair<int,int>(x,y))&&hash_list.count(pair<int,int>(xx,yy))) { sum++; } } } cout<<sum/2<<endl; }}
0 0
- poj- 2002-Squares-哈希|除法散列法
- Squares - POJ 2002 哈希
- Squares - POJ 2002 哈希
- POJ 2002 Squares 哈希
- poj 2002 Squares 哈希
- POJ 2002 Squares 哈希
- poj 2002 Squares
- poj 2002 Squares
- POJ 2002 Squares
- poj 2002 Squares
- poj 2002 Squares
- poj-2002 Squares
- POJ 2002 Squares
- POJ 2002 Squares
- BIT1033 POJ 2002 Squares
- POJ 2002 Squares
- POJ 2002 Squares
- POJ 2002 Squares Hash
- java 反射
- Unable to find a team with the given Team ID 问题解决方法
- [Dubbo实战]dubbo + zookeeper + spring 实战
- [网狐]6603后台最高管理员密码修改
- Android WebView 因重定向无法正常goBack()解决方案
- poj- 2002-Squares-哈希|除法散列法
- java自学之路--开始就需要摸清楚的环境配置和JDK细节
- Swift版 ScrollView和UITouch事件冲突
- 状压DP问题
- iOS 简单使用富文本格式对一个字符串进行不同设置
- sobel和laplace算子
- Linux____文件的压缩与打包学习笔记
- sublime快捷键
- OC继承的工作机制。