ZeroJudge H660: DodgeBall

Ah-Mao is going to school this weekend to practice dodgeball with his classmates. The objective of dodgeball is to throw the ball and hit the opposing team's players on the field. The players on the field can choose to dodge or catch the ball when the opposing team's ball comes their way. If a player is hit by the ball and no one catches it, the player must move to the outer area and wait.

During dodgeball practice, Ah-Mao set a strategy for himself:

  1. Stand still: If the ball lands outside of Ah-Mao's catchable range, he will not move.
  2. Catch the ball: If the ball lands within his catchable range and the speed is slow enough, Ah-Mao will move to the ball's position and catch it.
  3. Dodge: If the ball lands within his catchable range but is too fast, Ah Mao will move 15 units in the opposite direction of the ball (if the ball is on his left, he will move to the right). If the ball's position is the same as his position, he will dodge to the left.

Assuming Ah-Mao is at position 100, with a catching range extending 10 units to the left and right ([90, 110]), and the maximum catchable ball speed is 10. If the ball lands at position 99 with a speed of 5, Ah-Mao will choose to catch the ball and stand at position 99. However, if the ball lands at position 99 with a speed of 20, Ah-Mao will dodge to position 115 (100 + 15).

Please write a program to calculate Ah-Mao's final position after a series of dodgeball practices.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line of input contains three integers X, R, and V (0 ≤ X ≤ 1000, 0 ≤ R ≤ 500, 1 ≤ V ≤ 50), representing Ah-Mao's position, the catching range extending left and right, and the maximum catchable ball speed, respectively.
The second line contains an integer N (1 ≤ N ≤ 100), representing the number of practice throws. Following this, there are N lines, each containing two integers P and S (0 ≤ P ≤ 1000, 0 ≤ S ≤ 50), representing the position where the ball lands and the speed of the ball, respectively. Each pair of integers on the same line is separated by a space.
The output should be a single integer representing Ah-Mao's final position after the practice sessions.
100 10 10
3
99 5
97 3
88 4
88
500 80 15
5
400 10
420 5
300 20
395 25
435 18
420
20 10 5
3
30 8
15 2
20 10
0
ZeroJudge H660 Sample Test Cases

Thought Process

When inputting each new P and S, declare two variables, left and right, which represent the current boundaries within which Ah-Mao can catch the ball.

If P is not within the range of left and right, then no action is necessary.

If position P is within the range, first, check if the ball speed S exceeds the maximum catchable speed V. If S ≤ V, Ah Mao can catch the ball, so set X to P. If the ball's speed is too fast, determine which direction to dodge. If P ≥ X, indicating the ball is to the right, Ah-Mao should dodge left (X−=15). Here, using ">=" ensures that if P equals X, he dodges left as specified. If the ball is to the left, move right (X+=15).

Finally, output X.

Sample Code-ZeroJudge H660: DodgeBall

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int X, R, V, N;
    cin >> X >> R >> V >> N;
    for (int i = 0; i<N; i++) {
        int P, S, left = X - R, right = X + R;
        cin >> P >> S;
        if (P < left || P > right) continue;
        if (S <= V) {
            X = P;
            continue;
        }
        if (P >= X) X -= 15;
        else X += 15;
    }
    cout << X << "\n";
}

//ZeroJudge H660
//Dr. SeanXD

Comments