The Zombie Apocalypse case study - JavaScript

A nasty zombie virus is spreading out in the digital cities. We work at the digital CDC and our job is to look over the city maps and tell which areas are contaminated by the zombie virus so the digital army would know where to drop the bombs.

They are the new kind of digital zombies which can travel only in vertical and horizontal directions and infect only numbers same as them.

We'll be given a two-dimensional array with numbers in it.

For some mysterious reason patient zero is always found in north west area of the city (element [0][0] of the matrix) and the plague spreads from there to other cells by moving left, right, up or down.

We must create a function that returns a map (2-dimensional array) with all the contaminated areas marked as 1 and virus-free marked as 0.

In other words, we have to find all the matrix elements with the same value as [0][0] that we can go to by moving only down, up, right or left from [0][0] - without going into a field storing any other value.

Problem Visualization

Original Matrix: 9 9 9 1 2 Contamination Map: 1 1 1 0 0 Legend: Patient Zero & Connected Values Contaminated Area (1) Safe Area (0)

Algorithm Approach

The solution uses a flood-fill algorithm (similar to paint bucket tool) that:

  1. Starts from patient zero at [0][0]
  2. Builds a tree of all connected cells with the same value
  3. Marks contaminated areas as 1, safe areas as 0

Implementation

const arr = [
    [9, 1, 2, 3, 4, 1, 2, 9],
    [9, 9, 9, 2, 1, 5, 9, 9],
    [9, 2, 9, 3, 7, 9, 1, 9],
    [6, 9, 9, 9, 0, 9, 2, 9],
    [5, 4, 3, 9, 9, 9, 4, 9],
    [9, 3, 9, 5, 8, 9, 9, 9],
    [9, 9, 9, 9, 9, 9, 7, 9],
    [9, 9, 1, 2, 3, 9, 8, 9]
];

const findZombies = arr => {
    let i, j, result = [],
    zombie = arr[0][0],
    tree = {};
    
    const chance = ([i, j]) => {
        if (!tree[i] || !tree[i][j]) return;
        result[i][j] = 1;
        var temp = tree[i][j];
        tree[i][j] = undefined;
        temp.forEach(chance);
    }
    
    for (i = 0; i < arr.length; i++) {
        result.push([]);
        for (j = 0; j < arr[i].length; j++) {
            result[i].push(0);
            if (arr[i][j] !== zombie) continue;
            if (!tree[i]) tree[i] = {};
            tree[i][j] = [[i, j - 1], [i, j + 1], [i - 1, j], [i + 1, j]].filter(([x, y]) => arr[x] && arr[x][y] === zombie);
        };
    };
    
    chance([0, 0]);
    return result;
};

console.log(findZombies(arr));
[
  [
    1, 0, 0, 0,
    0, 0, 0, 1
  ],
  [
    1, 1, 1, 0,
    0, 0, 1, 1
  ],
  [
    1, 0, 1, 0,
    0, 1, 0, 1
  ],
  [
    0, 1, 1, 1,
    0, 1, 0, 1
  ],
  [
    0, 0, 0, 1,
    1, 1, 0, 1
  ],
  [
    1, 0, 1, 0,
    0, 1, 1, 1
  ],
  [
    1, 1, 1, 1,
    1, 1, 0, 1
  ],
  [
    1, 1, 0, 0,
    0, 1, 0, 1
  ]
]

How It Works

The algorithm works in three phases:

  1. Tree Building: Creates a graph where each cell with the zombie value (9) knows its connected neighbors
  2. Initialization: Sets up result matrix with all zeros
  3. Flood Fill: Starting from [0][0], recursively marks all reachable cells as contaminated (1)

Key Points

  • Only cells with the same value as [0][0] can be infected
  • Infection spreads only horizontally and vertically (not diagonally)
  • Isolated zombie cells that can't be reached from [0][0] remain uninfected
  • The result is a binary map: 1 for contaminated, 0 for safe

Conclusion

This zombie apocalypse case study demonstrates flood-fill algorithms in JavaScript. The solution efficiently maps contaminated areas using recursive traversal and produces a clear contamination map for strategic planning.

Updated on: 2026-03-15T23:18:59+05:30

307 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements