← Back to blog

Simulation Problems in Competitive Programming

·
competitive-programmingalgorithmsproblem-solving

What are Simulation Problems?

In competitive programming, we often come across what we call simulation problems—types of problems where the straightforward implementation of the given rules or directions is sufficient to find the solution. No need for complex algorithms or tricks; just execute the instructions as they are to get the answer.

In the realm of CP, they’re used to assess one’s fluency in the programming language of choice. One could say all simulation problems can be rephrased to: “Hi contestant, can you translate these instructions into code?”

Can you? Yes, you! Try this one:

Alice and Bob are standing on a 2D plane. Alice starts at the point (0, 0), and Bob starts at the point (R, S) where 1 ≤ R, S ≤ 1000. Every second, Alice moves M units to the right and N units up. Every second, Bob moves P units to the left and Q units down (1 ≤ M, N, P, Q ≤ 10). Determine if Alice and Bob will ever meet (be at the same point at the same time), and if so, when.

Input: The first line contains R and S. The second line contains M, N, P, and Q.

Output: A single integer—the number of seconds after which Alice and Bob meet. If they never meet, output −1.

Make sure you attempt the problem before continuing!


We are given the initial positions and speeds of Alice and Bob. We want to know if they will ever meet at the same point at the same time, and if so, when.

This looks, smells, and feels like a simulation problem. Why? We’re given a set of rules and tasked to know what’s the outcome of such rules after a certain number of steps (1000). So we can simulate their movements by using a loop that repeats 1000 times, or until they meet or go out of bounds.

In each iteration of the loop, we update their positions based on their speeds and check if they are at the same point. If they are, we print the current time as the answer. If they are not, we continue the loop. If we finish the loop without finding an answer, we print -1.

# Read R and S from input
# Set x1 = 0 and y1 = 0 (Alice's position)
# Set x2 = R and y2 = S (Bob's position)
# Read M, N, P and Q from input
# Set t = 0 (time)

# Start a loop that repeats 1000 times
    # Update x1 by adding M to it
    # Update y1 by adding N to it
    # Update x2 by subtracting P from it
    # Update y2 by subtracting Q from it
    # Increment t by 1

    # Check if x1 == x2 and y1 == y2
        # If yes, print t as the answer and exit
        # If no, continue the loop

    # Check if x1 or y1 > 1000 or x2 or y2 < 0
        # If yes, print -1 as the answer and exit
        # If no, continue the loop

# End the loop

How to detect a simulation problem?

How can we recognize a simulation problem? There are two easy clues:

  1. The problem gives you explicit step-by-step rules — If the problem statement reads like a recipe or a set of instructions that you just need to follow, it’s likely a simulation problem.

  2. Small constraints — Simulation problems typically have small input bounds (like 1000 in our example) because they’re meant to be solved by brute-force iteration, not clever optimization.

The key insight is that simulation problems test your ability to implement correctly, not your ability to optimize. The algorithm is given to you—your job is to translate it into working code without bugs.

When you see a problem and think “I just need to do exactly what they’re asking, step by step”—that’s a simulation problem.