A15315 | Deadly Laser
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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) \rightarrow (x, y + 1)$ ;
- $(x, y) \rightarrow (x + 1, y)$ ;
- $(x, y) \rightarrow (x, y - 1)$ ;
- $(x, y) \rightarrow (x - 1, y)$ .
The robot can't move outside the grid.
The cell $(s_x, s_y)$ 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 $(x_1, y_1)$ and $(x_2, y_2)$ is $|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)$ 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.
In one step, it can move into a cell, adjacent by a side to the current one:
- $(x, y) \rightarrow (x, y + 1)$ ;
- $(x, y) \rightarrow (x + 1, y)$ ;
- $(x, y) \rightarrow (x, y - 1)$ ;
- $(x, y) \rightarrow (x - 1, y)$ .
The robot can't move outside the grid.
The cell $(s_x, s_y)$ 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 $(x_1, y_1)$ and $(x_2, y_2)$ is $|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)$ 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.
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of testcases.
The only line of each testcase contains five integers $n, m, s_x, s_y, d$ ( $2 \le n, m \le 1000$ ; $1 \le s_x \le n$ ; $1 \le s_y \le m$ ; $0 \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 ( $(s_x, s_y) \neq (1, 1)$ and $(s_x, s_y) \neq (n, m)$ ). The starting cell $(1, 1)$ always has distance greater than $d$ to the laser ( $|s_x - 1| + |s_y - 1| > d$ ).
The only line of each testcase contains five integers $n, m, s_x, s_y, d$ ( $2 \le n, m \le 1000$ ; $1 \le s_x \le n$ ; $1 \le s_y \le m$ ; $0 \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 ( $(s_x, s_y) \neq (1, 1)$ and $(s_x, s_y) \neq (n, m)$ ). The starting cell $(1, 1)$ always has distance greater than $d$ to the laser ( $|s_x - 1| + |s_y - 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
3 2 3 1 3 0 2 3 1 3 1 5 5 3 4 1
输出 #1
3 -1 8
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted