Off white cow May 18 Forsaken race like squares solution to a problem (simple computational geometry)

Topic Link

Subject to the effect

There are four points, four points can be formed directly if a square, the output "wen". If a point (x, y) at this point into the (x + 1, y), (x-1, y), (x, y + 1), (x, y-1)

It may constitute the output square "hai xing". Otherwise, output "wo jue de bu xing".

Topic ideas

Foreword

The subject looks very simple, but that it wa a 5 rounds to write about an hour never before. . . . .

I was thinking rectangular square, diamond, etc. operations. . .

Sei题

In fact, once you have a look to the method.

First, there is a method of error to find the midpoint of a distance equal to four points to determine whether a square, but this can only be determined whether a rectangular shape.

In fact, the real process is determined directly from the four edges 6 point, if a small four equal sides, it can be determined that the diamond. The big two sides

Is diagonal, then you can determine if they are equal rectangular.

prompt

1: The special judge whether it coincides

2: it can be directly further not consider this square error is always such things floating point calculating side length

Code

#include<cstdio>
#include<vector> 
#include<algorithm>
#define fi first
#define se second
using namespace std;
int x[]={0,1,-1,0,0},y[]={0,0,0,1,-1};
pair<int,int> a[5];
bool check(){
	vector<int> s; 
	if(a[1].fi==a[2].fi&&a[1].se==a[2].se){//防止出现四点重合的情况 
		return false;
	} 
	for(int i=1;i<=4;i++){
		for(int j=i+1;j<=4;j++){
			s.push_back((a[i].fi-a[j].fi)*(a[i].fi-a[j].fi)+(a[i].se-a[j].se)*(a[i].se-a[j].se));//没必要根号操作,直接比较平方即可 
		}
	}
	sort(s.begin(),s.end());
	if(s[0]==s[3]&&s[4]==s[5]){
		return true;
	}else{
		return false;	
	}
}
int main(){
	for(int i=1;i<=4;i++){
		scanf("%d %d",&a[i].fi,&a[i].se);
	}
	if(check()){
		printf("wen\n");
		return 0;
	}else{
		for(int i=1;i<=4;i++){
			for(int j=1;j<=4;j++){
				a[i].fi+=x[j];
				a[i].se+=y[j];
				if(check()){
					printf("hai xing\n");
					return 0;
				}
				a[i].fi-=x[j];
				a[i].se-=y[j];
			}
		}
	}
	printf("wo jue de bu xing\n");
	return 0;
}
Published 68 original articles · won praise 2 · Views 2244

Guess you like

Origin blog.csdn.net/m0_46209312/article/details/105258673