[Python] Blue Crisis contamination simulation coding problem

Discussão em 'Python' iniciado por Stack, Junho 29, 2025.

  1. Stack

    Stack Membro Participativo

    Problem Statement:


    Blue Crisis: Real-Time Contamination Simulation and Isolation A clean water grid is under threat from chemical contamination spreading due to water flow. You are given:

    The contamination levels in each cell of an R × C grid.

    A water flow map indicating if water flows from that cell (flow[j] > 0) or not.

    A purification level at each cell to counter contamination.

    You must simulate the contamination spread and purification process over T time units. After that, perform threat analysis on the resulting grid:

    Count the number of danger zones — cells with contamination ≥ DANGER_LEVEL.

    Locate the most severely contaminated cell.

    Estimate the minimum number of sectors (neighboring safe cells) that need to be shut down to prevent further spread from dangerous zones.

    Input Format: The first line of the input consists of four integers: R, C, T, and DANGER_LEVEL (Grid dimensions, time steps, contamination threshold)

    The next R lines contain: Contamination grid (each line has C integers)

    The next R lines contain: Flow grid (each line has C integers; > 0 means water can spread)

    The next R lines contain: Purification grid (each line has C integers; amount reduced each step)

    Output Format: The first line of the output prints the number of danger zones after T time steps.

    The second line prints the coordinates of the most contaminated cell (as (i,j)).

    The third line prints the minimum number of safe sectors to shut down (neighbors of dangerous zones).

    Refer to the sample output for formatting specifications.

    Code Constraints: The given test cases fall under the following constraints:

    1 ≤ R, C ≤ 100

    0 ≤ T ≤ 100

    0 ≤ contamination[j], purify[j] ≤ 100

    0 ≤ flow[j] ≤ 1

    0 ≤ DANGER_LEVEL ≤ 100

    Sample Test Cases: Input 1:

    2 2 1 50 10 20 30 40 0 0 0 0 5 5 5 5

    Output 1:

    0 (1,1) 0

    Input 2:

    4 4 3 60 60 50 40 30 30 40 50 60 70 80 90 100 55 60 70 80 0 1 1 1 1 1 1 1 1 -1 -1 0 0 -1 0 0 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

    Output 2:

    12 (0,1) 4
    ​


    MY code(Does not pass every test case):

    R, C, T, D = map(int, input().split())
    grid = [list(map(int, input().split())) for _ in range(R)]
    flow = [list(map(int, input().split())) for _ in range(R)]
    purify = [list(map(int, input().split())) for _ in range(R)]

    dirs = [(-1,0), (1,0), (0,-1), (0,1)]

    # Simulation
    for _ in range(T):
    new_grid = [row[:] for row in grid]
    for i in range(R):
    for j in range(C):
    if flow[j] > 0 and grid[j] > 0: # Only spread if contamination exists
    for di, dj in dirs:
    ni, nj = i + di, j + dj
    if 0 <= ni < R and 0 <= nj < C:
    new_grid[ni][nj] += grid[j]

    for i in range(R):
    for j in range(C):
    new_grid[j] = max(0, new_grid[j] - purify[j])
    grid = new_grid

    # Count danger zones
    danger = 0
    dangerous_cells = []
    for i in range(R):
    for j in range(C):
    if grid[j] >= D:
    danger += 1
    dangerous_cells.append((i, j))

    # Find most contaminated cell (handle ties by choosing first occurrence)
    max_val = -1
    max_pos = (0, 0)
    for i in range(R):
    for j in range(C):
    if grid[j] > max_val:
    max_val = grid[j]
    max_pos = (i, j)

    # Count safe neighbors of dangerous zones
    safe_neighbors = set()
    for i, j in dangerous_cells:
    for di, dj in dirs:
    ni, nj = i + di, j + dj
    if 0 <= ni < R and 0 <= nj < C and grid[ni][nj] < D:
    safe_neighbors.add((ni, nj))

    print(danger)
    print(f"({max_pos[0]},{max_pos[1]})")
    print(len(safe_neighbors))

    Continue reading...

Compartilhe esta Página