Akash and Grid solution codechef
Akash is stuck in agrid, where is odd. The rows of the grid are numbered to from top to bottom, and the columns are numbered to from left to right. The cell at the intersection of the -th row and -th column will be denoted .
The grid has a unique center cell —. For example, when the center is cell .
Akash is currently at cell. He would like to reach the exit of the grid, which is located at the center. It is guaranteed that is not the center.
Suppose Akash is at cell. He can make the following movements:
- He can freely move along diagonals, i.e, to cells
- He can move one step horizontally or vertically with the cost of gold coin, i.e, to cells
Note that Akash is not allowed to make a move that will take him out of bounds of the grid.
Akash would like to minimize the number of coins required to reach the center. Please help him find this number.
- The first line of input contains a single integer , denoting the number of test cases. The description of test cases follows.
- Each test case consists of a single line of input, containing three space-separated integers — the size of the grid and the coordinates of Akash’s starting cell.
For each test case, output in a single line the minimum number of gold coins Akash needs to reach the center.
- is always odd.
Sample Input 1
2 3 2 1 5 3 1
Sample Output 1
Test case 1: For agrid, the center is at . It is not possible to reach from using only diagonal moves. So, Akash will directly go to the center using 1 gold coin.
Test case 2:, so the center is . Akash can go from to with a diagonal move, then from to with another diagonal move. So, he needs zero coins.