编程介的小学生 2019-08-19 22:54 采纳率: 0.4%
浏览 104

Special Robot的程序的编写思路

Problem Description
Bob invented a special kind of robot controlled by a remote controller. The interesting thing is that the robot can only move in a 2-dimentional plane which is vertical to the ground. Now we assume the vertical plane can be shown in Figure 1.

Now Bob wants to conduct a performance for his robot. The situation can be described as follows: n balloons stay still on the ground; all of these balloons have an initial position whose Y-coordinates are 0.
Each balloon will move up at certain time. The initial position of the robot is at (0, 0) and if the robot is at position (x, y) at time t, then it can only move to position (x, y-1) or position (x+1, y+1) in a unit time (the robot con not stay still at a position). Its task is to collect as more balloons as possible when it gets to the destination (K,0).

When the robot is moving on the plane, balloons are continuously rising on its path from the ground. Each balloon can reach a unit height in a unit time.
When the robot meets a balloon, it collects the balloon. The situation can be described as the following three examples which are shown in Figure 2:

Example 1: At time t, the robot is at position (x, y), and a balloon is at position (x, y-2). The robot moves down and the balloon moves up; then at time t+1, the robot and the balloon meet at position (x, y-1), so the balloon can be collected by the robot ( which is shown in Figure 2(a)).

Example 2: At time t, there is a balloon at position (x, y-1) and the robot stands at position (x, y). Because the balloon always moves up and the robot can move down, so the robot can collect this balloon because they will meet at some point between position (x, y-1) and (x, y) during their movement (which is shown in Figure 2(b)).

Example 3: At time t, a balloon is at position (x+1, y) and the robot is at position (x, y). Because the robot can arrive at position (x+1, y+1) while the balloon can move up to the same position, the robot could collect the balloon at time t+1 (which is shown in Figure 2(c)).

Maybe it is too easy a problem to control one robot, so Bob intends to control two robots simultaneously.
You need to help him to calculate the maximum balloons these two robots can collect when they get to the destination.

It is possible that the two robots get to the same position at the same time. Note that one balloon could not be collected by two robots and the balloons staying on the ground could not be collected either.

Robot should not move to the underground (y < 0).

Input
The input contains several cases. For each case, the first line gives the value of n (0 ≤ n ≤ 10,000) and K (1 ≤ K ≤ 100) followed by n lines in which each contains two integers x (1 ≤ x ≤ K) and t (0 ≤ t ≤ 1000).

The first integer indicates the position of a balloon and the other indicates the beginning time for it to rise. In the value of n and K, n is the number of balloons and K is the x-coordinate of the destination. The input is ended by two zeros.

Output
For each case, output the maximum balloons the two robots can collect.

Sample Input
3 2
1 1
1 1
1 2
7 4
2 0
2 3
3 4
3 0
3 2
4 0
1 1
0 0

Sample Output
2
6

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-11 10:19
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取数据
    n = readLines("input.txt")[[1]][1]
    k = readLines("input.txt")[[1]][2]
    
    # 初始化机器人和目标位置
    robot_x = 0
    robot_y = 0
    target_x = k
    target_y = 0
    
    # 计算最大可能收集到的气球数
    max_balloons = 0
    
    for i in 1:n {
        x = i - 1
        t = i - 1
        
        # 检查机器人是否已经到达目标位置
        if robot_x == target_x && robot_y == target_y {
            max_balloons += t + 1
            continue
        }
        
        # 检查机器人是否应该向下移动
        if robot_y >= 0 && robot_y <= target_y {
            robot_y -= 1
        } else {
            robot_x += 1
            robot_y = min(robot_y, target_y)
        }
    }
    
    print(max_balloons)
    

    请注意,上述代码是用R语言编写的,并且使用了基本的数据结构(如列表和循环)。它首先读取输入文件中的数据,然后初始化机器人和目标位置。接着,它遍历每个气球的位置和开始时间,并根据情况调整机器人的位置。最后,计算并输出两个机器人可以收集的最大气球数量。

    评论

报告相同问题?