Runner Game (SPOJ - NPC2016A)

点击打开链接

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) )

Sample run

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;
}

水波。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值