CF1721B.Deadly Laser
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The robot is placed in the top left corner of a grid, consisting of n rows and m columns, in a cell (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)→(x+1,y);
- (x,y)→(x,y−1);
- (x,y)→(x−1,y).
The robot can't move outside the grid.
The cell (sx,sy) contains a deadly laser. If the robot comes into some cell that has distance less than or equal to d to the laser, it gets evaporated. The distance between two cells (x1,y1) and (x2,y2) is ∣x1−x2∣+∣y1−y2∣.
Print the smallest number of steps that the robot can take to reach the cell (n,m) without getting evaporated or moving outside the grid. If it's not possible to reach the cell (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 d to the laser.
机器人被放置在一个由 n 行 m 列组成的网格的左上角,即位置 (1,1)。
每一步,机器人可以移动到与当前所在格子上下左右相邻(即共享一条边)的格子中:
- (x,y)→(x,y+1);
- (x,y)→(x+1,y);
- (x,y)→(x,y−1);
- (x,y)→(x−1,y)。
机器人不能移出网格边界。
格子 (sx,sy) 中装有一台致命激光发射器。若机器人进入某个与该激光器的曼哈顿距离不超过 d 的格子,则会被瞬间蒸发。两个格子 (x1,y1) 与 (x2,y2) 之间的距离定义为 ∣x1−x2∣+∣y1−y2∣。
请输出机器人在不被蒸发且不移出网格的前提下,到达目标格子 (n,m) 所需的最少步数;若无法到达 (n,m),则输出 −1。
激光器所在格子既不是起点 (1,1),也不是终点 (n,m);且起点 (1,1) 到激光器的距离严格大于 d。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of testcases.
The only line of each testcase contains five integers n,m,sx,sy,d (2≤n,m≤1000; 1≤sx≤n; 1≤sy≤m; 0≤d≤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) and (sx,sy)=(n,m)). The starting cell (1,1) always has distance greater than d to the laser (∣sx−1∣+∣sy−1∣>d).
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例仅有一行,包含五个整数 n,m,sx,sy,d(2≤n,m≤1000;1≤sx≤n;1≤sy≤m;0≤d≤n+m)—— 分别表示网格的尺寸、激光所在单元格的坐标,以及激光的蒸发距离。
激光既不在起点单元格,也不在终点单元格(即 (sx,sy)=(1,1) 且 (sx,sy)=(n,m))。起点单元格 (1,1) 到激光的距离恒严格大于 d(即 ∣sx−1∣+∣sy−1∣>d)。
输出格式
For each testcase, print a single integer. If it's possible to reach the cell (n,m) from (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,1) 出发,在不被蒸发且不移出网格的前提下到达单元格 (n,m),则输出机器人到达该位置所需的最少步数;否则,输出 −1。
输入输出样例
输入#1
3 2 3 1 3 0 2 3 1 3 1 5 5 3 4 1
输出#1
3 -1 8
输入解题思路,AI测评打分。不知道怎么写?