CF1721B.Deadly Laser

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The robot is placed in the top left corner of a grid, consisting of nn rows and mm columns, in a cell (1,1)(1, 1) .

In one step, it can move into a cell, adjacent by a side to the current one:

  • (x,y)(x,y+1)(x, y) \rightarrow (x, y + 1) ;
  • (x,y)(x+1,y)(x, y) \rightarrow (x + 1, y) ;
  • (x,y)(x,y1)(x, y) \rightarrow (x, y - 1) ;
  • (x,y)(x1,y)(x, y) \rightarrow (x - 1, y) .

The robot can't move outside the grid.

The cell (sx,sy)(s_x, s_y) contains a deadly laser. If the robot comes into some cell that has distance less than or equal to dd to the laser, it gets evaporated. The distance between two cells (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is x1x2+y1y2|x_1 - x_2| + |y_1 - y_2| .

Print the smallest number of steps that the robot can take to reach the cell (n,m)(n, m) without getting evaporated or moving outside the grid. If it's not possible to reach the cell (n,m)(n, m) , print -1.

The laser is neither in the starting cell, nor in the ending cell. The starting cell always has distance greater than dd to the laser.

输入格式

The first line contains a single integer tt ( 1t1041 \le t \le 10^4 ) — the number of testcases.

The only line of each testcase contains five integers n,m,sx,sy,dn, m, s_x, s_y, d ( 2n,m10002 \le n, m \le 1000 ; 1sxn1 \le s_x \le n ; 1sym1 \le s_y \le m ; 0dn+m0 \le d \le n + m ) — the size of the grid, the cell that contains the laser and the evaporating distance of the laser.

The laser is neither in the starting cell, nor in the ending cell ( (sx,sy)(1,1)(s_x, s_y) \neq (1, 1) and (sx,sy)(n,m)(s_x, s_y) \neq (n, m) ). The starting cell (1,1)(1, 1) always has distance greater than dd to the laser ( sx1+sy1>d|s_x - 1| + |s_y - 1| > d ).

输出格式

For each testcase, print a single integer. If it's possible to reach the cell (n,m)(n, m) from (1,1)(1, 1) without getting evaporated or moving outside the grid, then print the smallest amount of steps it can take the robot to reach it. Otherwise, print -1.

输入输出样例

  • 输入#1

    3
    2 3 1 3 0
    2 3 1 3 1
    5 5 3 4 1

    输出#1

    3
    -1
    8
首页