Week11

求正方形的代码,只要枚举两个点就可以了。

#include 
using namespace std;
const int N = 5 * 1e3 + 10;
int x[N],y[N];
bool f[N][N];
int main(){
	int n;
	int ans = 0;
	cin >> n;
	for(int i = 1;i <= n;i ++)
		cin >> x[i] >> y[i],f[x[i]][y[i]] = true;
	for(int i = 1;i <= n;i ++)
		for(int j = 1;j <= n;j ++){
			if(i == j) continue;
			int a = x[i] - x[j];
			int b = y[i] - y[j];
			int ax = x[i] - b;
			int bx = x[j] - b;
			int ay = y[i] + a;
			int by = y[j] + a;
			if(ax < 0 || ax > 5000 || bx < 0 || bx > 5000 || ay < 0 || ay > 5000 || by < 0 || by > 5000) continue; 
			if(f[ax][ay] && f[bx][by])
				ans = max(ans,a * a + b * b);
		}
	cout << ans;
	return 0;
}

你可能感兴趣的:(大一算法学习,c++,算法,数据结构)