hdoj 6055(2017 Multi-University Training Contest

来源:互联网 发布:问卷星数据分析 编辑:程序博客网 时间:2024/06/05 03:28

题目链接:Regular polygon

题目大意:给你一堆点,然后问能够用里面组成多少个正多边形

题目思路:因为是整数点,只能形成正四边形,所以枚举两个点,套公式找出另外两个点,然后直接判断就好,当然因为点很小,可以直接判断,用pair很轻松,当然如果是poj 2002,就需要用hash去判断
ps:正方形已知两个点,算另外两个点的公式:
已知: (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)

直接判断:

#include <map>#include <set>#include <cmath>#include <vector>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define ll long long#define mk make_pair#define y1 yyyusing namespace std;const int N = 1e3 + 5;map< pair<int, int>, bool> M;int x[N], y[N], n, ans;int main() {    int T;    while (scanf("%d", &n) != EOF) {        ans = 0;        M.clear();        for (int i = 1; i <= n; i++) {            scanf("%d %d", x + i, y + i);            M[mk(x[i], y[i])] = 1;        }        for (int i = 1; i <= n; i++) {            for (int j = i + 1; j <= n; j++) {                int dx = y[j] - y[i];                int dy = x[i] - x[j];                int ok = 0;                if (M.count(mk(x[i] + dx, y[i] + dy))) ok++;                if (M.count(mk(x[j] + dx, y[j] + dy))) ok++;                if (ok == 2) ans++;                ok = 0;                if (M.count(mk(x[i] - dx, y[i] - dy))) ok++;                if (M.count(mk(x[j] - dx, y[j] - dy))) ok++;                if (ok == 2) ans++;            }        }        printf("%d\n", ans / 4);    }}

hash:

#include <cmath>#include <cstring>#include <cstdlib>#include <cstdio>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int N = 1010;const int H = 10007;int ptx[N],pty[N],cur,n,hashTable[H];ll ans;struct Node{    int x,t,next;}node[N];void initHash(){    for(int i = 0;i < H;i++) hashTable[i] = -1;    cur = ans = 0;}void insertHash(int x,int y){    int h = (x*x+y*y)%H;    node[cur].x = x;    node[cur].y = y;    node[cur].next = hashTable[h];    hashTable[h] = cur;    cur++;}bool searchHash(int x,int y){    int h = (x*x+y*y)%H;    int next = hashTable[h];    while(next != -1){        if(x == node[next].x&&y = node[next].y) return true;    }    return false;}int main(){    while(~scanf("%d",&n)){        initHash();        for(int i = 0;i < n;i++){            scanf("%d%d",&ptx[i],&pty[i]);            insertHash(ptx[i],pty[i]);        }        for (int i = 0; i < n; ++i){            for (int j = i + 1; j < n; ++j){                int x1 = ptx[i] - (pty[i] - pty[j]);                int y1 = pty[i] + (ptx[i] - ptx[j]);                int x2 = ptx[j] - (pty[i] - pty[j]);                int y2 = pty[j] + (ptx[i] - ptx[j]);                if (searchHash(x1, y1) && searchHash(x2, y2)) ++ans;            }        }        for (int i = 0; i < n; ++i){            for (int j = i + 1; j < n; ++j){                int x1 = ptx[i] + (pty[i] - pty[j]);                int y1 = pty[i] - (ptx[i] - ptx[j]);                int x2 = ptx[j] + (pty[i] - pty[j]);                int y2 = pty[j] - (ptx[i] - ptx[j]);                if (searchHash(x1, y1) && searchHash(x2, y2)) ++ans;            }        }        printf("%lld\n",ans);    }    return 0;}