Minesweeper
Google Code Jam
programming competition
2014
algorithm challenge

Minesweeper master from Google Code Jam2014 Qualification round

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The Google Code Jam is an annual international coding competition hosted by Google. It tests the algorithmic problem-solving abilities of programmers. One of the challenges from the qualification round in 2014 was the "Minesweeper Master" problem. This problem tests the participant’s ability to process grids and manage special cases efficiently, drawing parallels to the popular Minesweeper game.

Problem Description

The Minesweeper Master problem provides a grid of cells where some cells contain mines, and others are empty. The challenge is to determine if it’s possible to construct a valid Minesweeper game configurations using the given constraints, and if so, output one such configuration.

The main objectives are:

  • Find one valid Minesweeper configuration or determine it isn't possible.
  • Design the grid in a format where there's one clickable cell that reveals the entire board if clicked. This is similar to guaranteed clearing in the corner of a Minesweeper game.

Input Details

The input for the problem consists of multiple test cases. Each includes:

  • A single integer `T`, the number of test cases.
  • For each test case:
    • Three integers: `R` (number of rows), `C` (number of columns), and `M` (number of mines).

Output Specification

For each test case:

  • Either output the Minesweeper grid or specify "Impossible" if a valid configuration can't be constructed.
  • The grid should indicate mines with "*" and empty spaces with ".", and the cell to click initially with `c`.

Example

A simplified input example:

4 5 8 2 2 1

  • For non-square grids or when `R` or `C` equals 1, the solution can be relatively simple — they only offer one stretch for mines placement.
  • When `M` equals 0 or `R*C - 1`, the grid only has either mines or safe spaces, making it straightforward to solve.
  • Determine if a checkered flag structure works (placing mines in 2x2 blocks or using concentric shapes).
  • Use matrix manipulation to validate if a single 'click' position will reveal all the non-mine areas.
  • M=R*C-1 (One clear cell, all others are mines), which needs careful handling especially for larger grids.
  • R = 1 or C = 1 (which describes a full row or column run).
  • Clickable positions (`c`) forming a safe starting point need priority handling.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.