编程介的小学生 2017-09-04 15:11 采纳率: 20.5%
浏览 756
已采纳

Catch The Heart

Problem Description
Do you know Popeye? He likes spinach(菠菜) very much. Maybe you have played some games about him. Now here comes one game.

There are many boards, on one board there is a heart, Popeye wants to catch the heart and send it to his GF, Oliver, so that, she will be happy.

At the beginning, Popeye is on one board, and the heart is on another board. Every board is described as h, l, and r, the height, the left end and the right end. You can look it as a line (l, h) (r, h) in XY coordinate system. Popeye can walk on the board, jump from one board to another.

Notes:
1.He can walk on a board 1 unit per second.
2.He can only jump vertically(垂直地), and it takes 1s for each jumping.
3.He can jump only 1 unit high, so that he can jump only if the difference of the boards’ height is 1.
4.He can only jump up to the left or right point of the board.
5.He can only jump down from the left or right point of the board.

You can look the picture:

The problem is, find the shortest time to catch the heart.

Input
The first line of the input contains an integer T (T <= 20) which means the number of test cases. For each case, first line is an integer m (2 <= m <= 10000) which means the number of the boards. Then comes two lines, the first line contains h0, l0, r0, p0 (l0 <= p0 <= r0), which mean the information of the board and the position that the heart on it. Next line contains h1, l1, r1, p1 (l1 <= p1 <= r1), which mean the information of the board and the position that Popeye on it. Then comes m-2 lines, each contains hi, li, ri mean the ith (2 <= i < m) board’s information.

There is no common point or common parts for every two boards.
All inputs are integers, for each board, 0 <= h <= 10000, 0 <= l < r <= 100000000.

Output
For each case, output the shortest time to catch the heart if Popeye can catch it, otherwise output -1.

Sample Input
3
3
3 4 5 5
1 0 10 0
4 1 2
2
1 1 99999999 1
0 0 100000000 100000000
5
4 0 5 0
0 0 5 0
1 5 10
2 6 15
3 5 10

Sample Output
-1
100000000
24

  • 写回答

1条回答

  • threenewbee 2017-09-18 21:26
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面