欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1715A.Crossmarket
Crossmarket
Stanley and Megan decided to shop in the "Crossmarket" grocery store, which can be represented as a matrix with rows and columns.
Stanley and Megan can move to an adjacent cell using unit of power. Two cells are considered adjacent if they share an edge. To speed up the shopping process, Megan brought her portals with her, and she leaves one in each cell she visits (if there is no portal yet). If a person (Stanley or Megan) is in a cell with a portal, that person can use unit of power to teleport to any other cell with a portal, including Megan's starting cell.
They decided to split up: Stanley will go from the upper-left cell (cell with coordinates ) to the lower-right cell (cell with coordinates ), whilst Megan needs to get from the lower-left cell (cell with coordinates ) to the upper-right cell (cell with coordinates ).
What is the minimum total energy needed for them both to do that?
Note that they can choose the time they move. Time does not affect energy.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The only line in the test case contains two integers and ().
Output
For each test case print a single integer on a new line – the answer.
Note

In the first test case they can stick to the following plan:
- Megan (red circle) moves to the cell . Then she goes to the cell , and Stanley (blue circle) does the same.
- Stanley uses the portal in that cell (cells with portals are grey) to get to the cell . Then he moves to his destination — cell .
- Megan also finishes her route and goes to the cell .
The total energy spent is , which is our final answer.
Samples
7
7 5
5 7
1 1
100000 100000
57 228
1 5
5 1
15
15
0
299998
340
5
5
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |