Gym 101064 A - Renzo and the lost artifact 题解

题意

After unfolding the mysteries of the palindromic decorations in the Thai ruins, the famous researcher Renzo “the fearless” Morales is searching for a Sioux artifact. Some people believe that such artifact got lost a long time ago during the struggles between the Sioux and the north-american settlers.
Renzo believes that this artifact was hidden from the north-americans, and after a long search, he found clues to its location. The clues are two old maps of the region that today is known as Rapid City. Both maps have rectangular shape and show exactly the same region, but one is on a larger scale than the other. Renzo noticed that the corners of the smaller map are numbered from 1 to 4, and in the interior of the larger map exist four points numbered from 1 to 4 forming a rectangle of the exact same size of the smaller map.
Being a proficient problem solver, the famous researcher came rapidly to the conclusion that the smaller map has to be put on top of the larger map, in such a way that the numbered points coincide. Considering that both maps are in the Cartesian plane with the origin at the lower left corner of the larger map, the place where the artifact is hidden must correspond to a point in the plane that represents the same location in both maps.
To help Renzo find this precious historic artifact, write a program that finds one of these points.
Input
The first line contains two integers H and W (10 ≤ H, W ≤ 1000), that represent the height and the width of the map, respectively. The following lines contain four pairs of values x i, y i (0 < x i < W, 0 < y i < H), one pair per line, indicating the coordinates of the smaller map, starting from its lower left corner (regarding the original orientation), followed by its lower right corner, upper right and finally the upper left corner. The coordinates are given with exactly 8 decimal places. Its guaranteed that the ratio between the scales of the smaller and larger map is between 0.01 and 0.99, inclusive.
Output
Print a single line containing two numbers x p, y p: the coordinates of the point corresponding to the location of the artifact in both maps. Absolute or relative errors up to 10 - 4 will be tolerated. If there are multiple solutions, print any of them. It is guaranteed that at least one exists.

题解

模拟退火(三分算法)
从(0,0)到(H,W)到目标点的距离是一个单峰函数,只要对距离进行三分就好啦!

#include<bits/stdc++.h>
using namespace std;
const double eps=1e-13;
struct Point{
	double x,y;
	Point(){}
	Point(double x,double y){
		this->x=x;
		this->y=y;
	}
}p[4],dir[4];
Point operator-(Point a,Point b){
	return Point(a.x-b.x,a.y-b.y);
}
Point operator+(Point a,Point b){
	return Point(a.x+b.x,a.y+b.y);
}
Point operator*(Point a,double k){
	return Point(a.x*k,a.y*k);
}
double W,H; 
double dist(Point a,Point b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} 
double T=1.0,t;bool  flag;
int g;
int main(){
	cin>>H>>W;
	for(int i=0;i<4;i++)
		cin>>p[i].x>>p[i].y;
	dir[0]=Point(0,H);
	dir[1]=Point(W,0);
	dir[2]=Point(0,0);
	dir[3]=Point(W,H);
    Point start=dir[2]; 
    double ans=dist(p[0],start);
    for(int k=1;k<=10000;k++){  
    	flag=1;
    	while(flag){ 
    		flag=0;
	    	for(int i=0;i<4;i++){
	        	Point now=start*(1.0-T)+dir[i]*T;//向四个点靠近 
		    	Point nxt=(p[1]-p[0])*(now.x/W)+(p[3]-p[0])*(now.y/H)+p[0];
		    	if(dist(nxt,now)+eps<ans){
			    	ans=dist(nxt,now);
			    	start=now;
			    	flag=1;
			    }
		    }
    	}
        T*=0.98;
    }
    if((fabs(ans)<1e-4)){
		printf("%.6lf %.6lf\n",start.x,start.y);	
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值