POJ 2002 Cuadrados resolver informe de hash

POJ 2002 Cuadrados informe de resolución de problemas

Significado de las preguntas: punto dado en el avión, encontrar el máximo número de puntos con estos cuadrados se puede construir.
Ideas de resolución de problemas: la violencia directa las ideas normales sin duda el tiempo de espera, que leen el blog de otras personas, de hecho, es utilizar el método discreto reduce en gran medida el tiempo de travesía. En otro lugar que no es tan difícil. También encontró que: nueva distribución de clase es inicializada automáticamente, la asignación de espacio de malloc solamente, el constructor de la clase no sirve para nada, próximos punteros tienen su propio asignado a NULL.
Aquí Insertar imagen Descripción

#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;
	}
}



Publicado 64 artículos originales · ganado elogios 0 · Vistas 1452

Supongo que te gusta

Origin blog.csdn.net/weixin_45566331/article/details/104675708
Recomendado
Clasificación