Today, Waca will play runner game. In this game, the player will be placed in a square-shaped arena where they will run to checkpoints of the arena.
Arena of the game is a square with a side length of N. The player will be placed in a coordinate (X,Y) inside the arena. The following are the rules of the game :
- Player will be initially at coordinate (X,Y), where coordinate (0,0) is the bottom left corner of the arena
- Then, player should run to the side of the arena to get the checkpoints
- Checkpoints are located in each side of the arena (top, left, bottom, right) and player should take all the checkpoints (i.e. they have to visit all sides), then go back to the initial position of the player (X,Y)
- If the player reach the corner of the arena, he can take 2 checkpoints. For example, if a player reach coordinate (0,0), he is considered to get the left and bottom checkpoints
Now, Waca is curious, what is the minimum distance to get all the checkpoints and go back to his initial position? The image below is one of the possible moves for the runner game. (The red circle is Waca's initial position (X,Y) )
Input
The input will contain 3 integers, X, Y, and N.
Output
Print the minimum distance Waca need to finish the game. Your answer is considered to be correct if the difference is no more than 10-6
Example
Input: 6 4 10 Output: 28.284271
Constraints:
- 1 ≤ N ≤ 106
- 0 ≤ X,Y ≤ N
题意很明确,让你求从一个点(x,y)到四条边各走一次再回到点(x,y)的最短距离。
解题思路:
其实做的时候我并没有思路,仅靠感觉蒙了一波,即过点(x,y)作斜率为1的直线交正方形于a,b两点;
然后关于正方形斜率为1的对角线对称得到正方形上的另外两个点c,d; 然后求由a,b,c,d构成的矩形的周长就是我们需要的结果。
如果有大佬可以证明,求教育。
上代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
double x, y ,n;
scanf("%lf %lf %lf", &x, &y, &n);
double t = y-x;
double a = sqrt((n-t)*(n-t) * 2.0 ) * 2.0 + sqrt(2.0)*t*2.0;
printf("%lf\n",a);
return 0;
}
水波。