ハッシュレポートを解決POJ 2002スクエア

POJ 2002二乗問題解決レポート

質問の意味:面内の所与の点を、これらの四角の点の最大数を構成することができます。
アイデア解決の問題:アイデア通常の直接暴力確かにタイムアウト、私は他の人のブログを読んで、実際には、離散的な方法を使用することです大幅に通過する時間を短縮します。他の所でそれはそれは難しいことではありません。また、ことがわかった:クラスの新しい分布が自動的に初期化され、空間のmalloc関数割り当てのみ、クラスのコンストラクタは無用で、次のポインタがNULLに割り当てられ、独自のを持っています。
ここに画像を挿入説明

#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<iomanip>
#pragma warning(disable:4996)
#define INF 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
const int N = 1000010;
const int maxn = 1e9;
using namespace std;
const int prime = 1999;//2000内最大素数
struct node {
	int x, y;
};
struct square {
	int x, y;
	square* next;
	square()
	{
		next = NULL;
	}
};
node pos[1005];//放原始数据
square* ha[2005];
void in(int t)
{
	int key = (pos[t].x * pos[t].x + pos[t].y * pos[t].y) % prime + 1;//+1是为了避开哈希表下标0
	if (NULL == ha[key])
	{
		square* tem = (square*)malloc(sizeof(square));
		tem->x = pos[t].x;
		tem->y = pos[t].y;
		tem->next = NULL;
		ha[key] = tem;
	}
	else
	{
		square* tem = ha[key];
		while (tem->next)
		{
			tem = tem->next;
		}
		tem->next = (square*)malloc(sizeof(square));
		tem->next->x = pos[t].x;
		tem->next->y = pos[t].y;
		tem->next->next = NULL;
	}
}
bool judge(int x, int y)
{
	int key = (x * x + y * y) % prime + 1;
	if (NULL == ha[key])
	{
		return false;
	}
	else
	{
		square* tem = ha[key];
		while (tem != NULL)
		{
			if (x == tem->x && y == tem->y)
				return true;
			tem = tem->next;
		}
	}
	return false;
}
int main()
{
	int n;
	while (cin >> n && n)
	{
		memset(ha, 0, sizeof(ha));
		for (int i = 1; i <= n; i++)
		{
			cin >> pos[i].x >> pos[i].y;
			in(i);
		}
		int ans = 0;
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = i + 1; j <= n; j++)
			{
				int a = pos[j].x - pos[i].x;
				int b = pos[j].y - pos[i].y;
				int x3 = pos[i].x + b;
				int y3 = pos[i].y - a;
				int x4 = pos[j].x + b;
				int y4 = pos[j].y - a;
				if (judge(x3, y3) && judge(x4, y4))
					ans++;
				x3 = pos[i].x - b;
				y3 = pos[i].y + a;
				x4 = pos[j].x - b;
				y4 = pos[j].y + a;
				if (judge(x3, y3) && judge(x4, y4))
					ans++;
			}
		}
		cout << ans / 4 << endl;
	}
}



公開された64元の記事 ウォンの賞賛0 ビュー1452

おすすめ

転載: blog.csdn.net/weixin_45566331/article/details/104675708