test1.cpp
上传用户:vipking888
上传日期:2015-02-03
资源大小:93k
文件大小:3k
- #include<iostream>
- #include<cmath>
- #include<time.h>
- using namespace std;
- struct node{double x,y,wucha;bool ok;}a[111],a1[111];
- bool c[111][111];
- int i,j,n,m,s,p,q,k,chang,kuan;
- double totwc,wucha,e[111][111];
- int jiefangcheng(int i0,int i1,int i2,int i3){
- double d1,d2,d3,y1,y2,x1,x2,t1,t2,t3,t4,o,x,y,a11,a12,a21,a22,zhi,p,q,b11,b12,b21,b22;
- if(e[i1][i2]+e[i2][i0]<e[i1][i0])return 0;
- p=2*(a[i2].x-a[i1].x);
- q=2*(a[i2].y-a[i1].y);
- o=e[i0][i1]*e[i0][i1]-e[i0][i2]*e[i0][i2]-a[i1].x*a[i1].x+a[i2].x*a[i2].x-a[i1].y*a[i1].y+a[i2].y*a[i2].y;
- if(abs(p)<1e-6){
- y2=y1=o/q;
- t1=1;
- t2=-2*a[i1].x;
- t3=a[i1].x*a[i1].x-e[i0][i1]*e[i0][i1]+(y1-a[i1].y)*(y1-a[i1].y);
- if(t2*t2-4*t1*t3<-1e-6)return 0;
- x1=(-t2+sqrt(t2*t2-4*t1*t3))/2/t1;
- x2=(-t2-sqrt(t2*t2-4*t1*t3))/2/t1;
- }else{
- t1=q*q/p/p+1;
- t2=-2*o*q/p/p+2*a[i1].x*q/p-2*a[i1].y;
- t3=o*o/p/p+a[i1].x*a[i1].x-2*a[i1].x*o/p+a[i1].y*a[i1].y-e[i0][i1]*e[i0][i1];
- if(t2*t2-4*t1*t3<-1e-6)return 0;
- y1=(-t2+sqrt(t2*t2-4*t1*t3))/2/t1;
- y2=(-t2-sqrt(t2*t2-4*t1*t3))/2/t1;
- x1=(o-q*y1)/p;
- x2=(o-q*y2)/p;
- }
- d1=(x1-a[i3].x)*(x1-a[i3].x)+(y1-a[i3].y)*(y1-a[i3].y);
- d2=(x2-a[i3].x)*(x2-a[i3].x)+(y2-a[i3].y)*(y2-a[i3].y);
- d3=e[i0][i3]*e[i0][i3];
- if(abs(d1-d3)<abs(d2-d3)){x=x1;y=y1;}else{x=x2;y=y2;}
- a1[i0].x=x;
- a1[i0].y=y;
- a1[i0].ok=1;
- return 1;
- }
- void cal(int k){
- int i,j,p,b[5],tmp=0;
- bool c[111],hasans=0;
- while(1){
- ++tmp;
- memset(c,0,sizeof(c));
- for(i=1;i<=3;i++){
- while(1){p=rand()%m+1;if(c[p]==0){c[p]=1;break;}}
- b[i]=p;
- }
- for(i=1;i<=3;i++)for(j=i+1;j<=3;j++)if(e[b[i]][k]<e[b[j]][k]){p=b[i];b[i]=b[j];b[j]=p;}
- if(jiefangcheng(k,b[1],b[2],b[3])){hasans=1;break;}else
- if(jiefangcheng(k,b[1],b[3],b[2])){hasans=1;break;}else
- if(jiefangcheng(k,b[2],b[3],b[1])){hasans=1;break;}
- if(tmp>10)break;
- }
-
- }
- int main(){
-
- srand(time(0));
- printf("请输入总节点个数,基站个数,误差值:");
- cin>>n>>m>>wucha;//m为基站个数
- printf("请输入网络区域的长、宽:");
- cin>>chang>>kuan;
- ++chang;++kuan;
- freopen("output1.txt","w",stdout);
- //m=10;wucha=0.3;
- for(i=1;i<=n;i++){
- while(1){
- p=rand()%chang;
- q=rand()%kuan;
- if(c[p][q]==0){
- c[p][q]=1;
- break;
- }
- }
- a[i].x=p;
- a[i].y=q;
- }
-
- //a[1].x=41;a[1].y=85;a[2].x=72;a[2].y=38;a[3].x=80;a[3].y=69;a[4].x=96;a[4].y=22;
- for(i=1;i<=n;i++)
- for(j=1;j<=n;j++)
- e[i][j]=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y))+wucha*10;
- for(i=m+1;i<=n;i++)cal(i);
-
-
-
- // for(i=1;i<=n;i++)cout<<a[i].x<<' '<<a[i].y<<endl;
-
- for(i=m+1;i<=n;i++)if(a1[i].ok)a1[i].wucha=sqrt((a1[i].x-a[i].x)*(a1[i].x-a[i].x)+(a1[i].y-a[i].y)*(a1[i].y-a[i].y));
- for(i=m+1;i<=n;i++)if(a1[i].ok)totwc+=a1[i].wucha;
- printf("总误差: ");
- cout<<totwc<<endl;
- for(i=1;i<=m;i++)
- a1[i].ok=1;
- // for(i=m+1;i<=n;i++)if(a1[i].ok){
- for(i=1;i<=n;i++)if(a1[i].ok){
- //printf("第%d个点的原始坐标: ",i);
- cout<<a[i].x<<' '<<a[i].y<<' '<<a1[i].x<<' '<<a1[i].y<<' '<<a1[i].wucha<<endl;
- //printf("算出的第%d个点的坐标和误差: ",i);
- //cout<<a1[i].x<<' '<<a1[i].y<<' '<<a1[i].wucha<<endl;
- }else cout<<"Cannot Find."<<endl;
-
- return(0);
- }