A15831 | Lame King
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a checkerboard of size $201 \times 201$ , i. e. it has $201$ rows and $201$ columns. The rows of this checkerboard are numbered from $-100$ to $100$ from bottom to top. The columns of this checkerboard are numbered from $-100$ to $100$ from left to right. The notation $(r, c)$ denotes the cell located in the $r$ -th row and the $c$ -th column.
There is a king piece at position $(0, 0)$ and it wants to get to position $(a, b)$ as soon as possible. In this problem our king is lame. Each second, the king makes exactly one of the following five moves.
- Skip move. King's position remains unchanged.
- Go up. If the current position of the king is $(r, c)$ he goes to position $(r + 1, c)$ .
- Go down. Position changes from $(r, c)$ to $(r - 1, c)$ .
- Go right. Position changes from $(r, c)$ to $(r, c + 1)$ .
- Go left. Position changes from $(r, c)$ to $(r, c - 1)$ .
King is not allowed to make moves that put him outside of the board. The important consequence of the king being lame is that he is not allowed to make the same move during two consecutive seconds. For example, if the king goes right, the next second he can only skip, go up, down, or left.What is the minimum number of seconds the lame king needs to reach position $(a, b)$ ?
There is a king piece at position $(0, 0)$ and it wants to get to position $(a, b)$ as soon as possible. In this problem our king is lame. Each second, the king makes exactly one of the following five moves.
- Skip move. King's position remains unchanged.
- Go up. If the current position of the king is $(r, c)$ he goes to position $(r + 1, c)$ .
- Go down. Position changes from $(r, c)$ to $(r - 1, c)$ .
- Go right. Position changes from $(r, c)$ to $(r, c + 1)$ .
- Go left. Position changes from $(r, c)$ to $(r, c - 1)$ .
King is not allowed to make moves that put him outside of the board. The important consequence of the king being lame is that he is not allowed to make the same move during two consecutive seconds. For example, if the king goes right, the next second he can only skip, go up, down, or left.What is the minimum number of seconds the lame king needs to reach position $(a, b)$ ?
输入格式
The first line of the input contains a single integer $t$ ( $1 \leq t \leq 10^4$ ) — the number of test cases. Then follow $t$ lines containing one test case description each.
Each test case consists of two integers $a$ and $b$ ( $-100 \leq a, b \leq 100$ ) — the position of the cell that the king wants to reach. It is guaranteed that either $a \ne 0$ or $b \ne 0$ .
Each test case consists of two integers $a$ and $b$ ( $-100 \leq a, b \leq 100$ ) — the position of the cell that the king wants to reach. It is guaranteed that either $a \ne 0$ or $b \ne 0$ .
输出格式
Print $t$ integers. The $i$ -th of these integers should be equal to the minimum number of seconds the lame king needs to get to the position he wants to reach in the $i$ -th test case. The king always starts at position $(0, 0)$ .
输入输出样例
输入 #1
5 -4 1 4 4 0 -6 -5 -4 7 -8
输出 #1
7 8 11 9 15
One of the possible solutions for the first example is: go down, go right, go down, go right, go down, go left, go down.
One of the possible solutions for the second example is to alternate "go right" and "go up" moves $4$ times each.
One of the possible solutions for the third example is to alternate "go left" and "skip" moves starting with "go left". Thus, "go left" will be used $6$ times, and "skip" will be used $5$ times.
One of the possible solutions for the second example is to alternate "go right" and "go up" moves $4$ times each.
One of the possible solutions for the third example is to alternate "go left" and "skip" moves starting with "go left". Thus, "go left" will be used $6$ times, and "skip" will be used $5$ times.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted