luogu#P9830. [ICPC2020 Shanghai R] Traveling in the Grid World
[ICPC2020 Shanghai R] Traveling in the Grid World
题目描述
Consider a grid pattern with rows and columns. There are grid points in total which is the intersections of horizontal lines and vertical lines. We number the horizontal lines from to from top to bottom. We number the vertical lines from to from left to right. The intersection of horizontal line and vertical line is named ().
There are some constraints when you travel in the grid world. When you are located at point , you can choose a destination and walk to it along the line segment between and . We call this operation a . A walk is forbidden if there exists another grid point different from and lying on the line segment between them. You can walk as many times as you want but the directions of two consecutive walks cannot be the same. (Specifically, if you walk from to and then walk from to , you must make sure that .) The length of a walk from to is defined as the Euclidean distance between the two endpoints, .
Starting from , you are planning to arrive at by several walks. Because of the annoying rules, you may need some turning points to achieve your goal. Please find the minimum total length of your walks.
输入格式
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains two integers () indicating the size of the grid graph.
It is guaranteed that the sum of the values of over all test cases does not exceed .
输出格式
For each test case, output the minimum total length of walks. Your answer will be considered correct if its absolute or relative error does not exceed .
题目大意
题目描述
考虑具有 行和 列的网格模式。总共有网格点,即水平线和垂直线的交点。我们将水平线从上到下从 到 编号。我们将垂直线从左到右编号从 到 。水平线 和垂直线 的交点命名为 ()。
当您在网格世界中旅行时,会有一些限制。当您位于点 时,您可以选择一个目的地 并沿着 和 之间的线段步行到该目的地。我们将此操作称为 。如果它们之间的线段上存在另一个不同于 和 的网格点,则禁止行走。您可以随心所欲地走很多次,但连续两次步行的方向不能相同。(具体来说,如果从 到 ,然后从 到 ,则必须确保 。从 到 的步行长度定义为两个端点之间的欧几里得距离 。
从 开始,您计划通过几次步行到达 。由于烦人的规则,您可能需要一些转折点来实现您的目标。请找到您步行的最小总长度。
输入格式
有多个测试用例。输入的第一行包含一个整数 ,表示测试用例的数量。对于每个测试用例, 第一行包含两个整数 (),表示网格图的大小。
保证所有测试用例中 的值之和不超过 。
2
2 2
2 3
3.236067977499790
3.605551275463989