HDU 4082 Hou Yi's secret-求相似三角形的最大个数-(坑货)

题意:找相似三角形的最大个数。注意不是所有相似三角形的个数,而是不同类相似三角形 中个数最大的

分析:

之前理解成了所有相似三角形的个数,所以尽管考虑了所有的特殊情况以及精度问题还是不停的wawawa,甚至重新写了一遍不用余弦来判断而是用边。绝望之中仔细看别人的代码,原来题意理解错了。

这题的收获:

1.三角形相似的判定:用余弦定理、或者边成比例。最好用边,然后判定的时候不用比值,用乘积,这样就不存在精度问题。

2.耐心、对自己有信心。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 1000000007
#define max(a,b) a>b?a:b
using namespace std;
const double eps=1e-9;
int n;
struct node1{
	double x,y;
}a[20];
node1 aa[20];
struct node2{
	double a1,a2,a3;
}m[1500];
int vis2[1500];
int vis3[20];
int main()
{
	 while(cin>>n){
	 	if(!n) break;
	 	memset(vis2,0,sizeof(vis2));
	 	memset(vis3,0,sizeof(vis3));
	 	memset(a,0,sizeof(a));
		int tot=0,cnt=0;
		int mx=0;
	 	for(int i=0;i<n;i++) cin>>aa[i].x>>aa[i].y;
	 	int len=n;
	 	n=0;
	 	for(int i=0;i<len;i++){
	 		if(!vis3[i]){
	 			a[n].x=aa[i].x;
		 		a[n++].y=aa[i].y;
		 		int j;
		 		for(j=i+1;j<len;j++)
		 		   if(aa[i].x==aa[j].x&&aa[i].y==aa[j].y) vis3[j]=1;
	 		}
	 	}
	 	for(int i=0;i<n;i++){
	 		for(int j=i+1;j<n;j++){
	 			for(int k=j+1;k<n;k++){
	 					double p=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
	 					double q=(a[i].x-a[k].x)*(a[i].x-a[k].x)+(a[i].y-a[k].y)*(a[i].y-a[k].y);
	 					double u=(a[j].x-a[k].x)*(a[j].x-a[k].x)+(a[j].y-a[k].y)*(a[j].y-a[k].y);
	 					double tmp1=(p+q-u)/(2*sqrt(p)*sqrt(q));
	 					double tmp2=(p+u-q)/(2*sqrt(p)*sqrt(u));
	 					double tmp3=(q+u-p)/(2*sqrt(u)*sqrt(q));
	 					if(tmp1>tmp2){
	 						double tmp=tmp1;
	 						tmp1=tmp2;
	 						tmp2=tmp;
	 					}
	 					if(tmp1>tmp3){
	 						double tmp=tmp1;
	 						tmp1=tmp3;
	 						tmp3=tmp;
	 					}
	 					if(tmp2>tmp3){
	 						double tmp=tmp2;
	 						tmp2=tmp3;
	 						tmp3=tmp;
	 					}
	 					double ll=abs(tmp1+1);
	 					if(ll>=eps){
	 						m[cnt].a1=tmp1;
		 					m[cnt].a2=tmp2;
		 					m[cnt++].a3=tmp3;
	 					}
	 			}
	 		}
	 	}
        if(cnt<=1) cout<<cnt<<endl;
	 	else{
	 		for(int i=0;i<cnt;i++){
	 			int ans=0;
		 		for(int j=i+1;j<cnt;j++){
		 			if(abs(m[i].a1-m[j].a1)<eps&&abs(m[i].a2-m[j].a2)<eps&&abs(m[i].a3-m[j].a3)<eps){
		 				ans++;
		 			}
		 		}
		 		mx=max(mx,ans);
		 	}
		 	cout<<mx+1<<endl;
	 	}
	 }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 4082 Hou Yi's secret-求相似三角形的最大个数-(坑货)

时间: 2024-12-28 14:37:03

HDU 4082 Hou Yi's secret-求相似三角形的最大个数-(坑货)的相关文章

[HDU 4082] Hou Yi&#39;s secret (简单计算几何)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4082 题目大意: 给你n个点,问能最多构成多少个相似三角形. 用余弦定理,计算三个角度,然后暴力数有多少个,更新答案. 代码: 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 #includ

HDU 4082 Hou Yi&#39;s secret --枚举

题意: 给n个点,问最多有多少个相似三角形(三个角对应相等). 解法: O(n^3)枚举点,形成三角形,然后记录三个角,最后按三个角度依次排个序,算一下最多有多少个连续相等的三元组就可以了. 注意:在同一个坐标的两点只算一次,所以要判一下. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #includ

HDU - 4082 Hou Yi&#39;s secret

题意:射箭落在n个点,任取三点可构成一个三角形,问最大的相似三角形集(一组互相相似的三角形)的个数. 分析: 1.若n个点中有相同的点,要去重,题目中说射箭会形成洞,任选三个洞构成三角形,因此射在同一点只形成一个洞. 2.二进制枚举子集选出三个点,判断能否构成三角形. 3.因为边长可能为小数,因此用边长的平方判断相似. (1)将比较的两个三角形三边分别排序 (2)tmpx[0] / tmpy[0] = tmpx[1] / tmpy[1] = tmpx[2] / tmpy[2],即若满足tmpx[

hdu4082 Hou Yi&#39;s secret 暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4082 Hou Yi's secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3006    Accepted Submission(s): 710 Problem Description Long long ago, in the time

hdu 1536 S-Nim 博弈论,,求出SG&#39;函数就可以解决

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4975    Accepted Submission(s): 2141 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Chan

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

HDU 4738 Caocao&#39;s Bridges(求价值最小的桥)

Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and