Minesweeper is a popular puzzle game where you must discover which tiles are "mines" without clicking on those tiles. Instead, you click on nearby tiles to reveal the number of adjacent mines. One downside about the game is that it is possible to end up in a scenario where there are multiple valid answers and you may only guess. For example, take the following board:


In this format, a number represents the number of adjacent mines, an * represents a known mine, and a "?" represents a potential mine. The unfortunate thing about this particular puzzle is that there are four distinct and valid potential solutions:

1110    1110    1110    1110    
2*31    2*31    2*31    2*31
3*4*    3*5*    3**2    3**1
2*42    2*4*    2*4*    2*42
112*    1121    1121    112*

This means the board is unsolvable. Here is an example of a solvable board:


This board is solvable because there is only one possible valid solution:


Your task is to write either a program or function that takes a valid minesweeper board and determines if it is solvable or not. By "valid minesweeper board", I mean that the input will always be rectangular, have at least one solution, and not contain any invalid characters.

Your input may be an array of characters, an array of strings, a string containing newlines, etc. The output must be a truthy value if it is solvable and a falsy value if it is not. I am not extremely worried about performance, but your solution must theoretically work for any size input.

As usual, standard loopholes apply and the shortest solution in bytes wins!


The following examples are all solvable:








The following examples are all unsolvable:




  • \$\begingroup\$ Can we assume the board is rectangular with at least one cell? Also, can we assume that the input will always admit at least one solution? (For example, 2? has no solutions, which means it cannot come from an actual game of Minesweeper. Hence it is not considered a "Minesweeper board"...yes?) \$\endgroup\$
    – mathmandan
    Dec 4, 2016 at 0:49
  • 3
    \$\begingroup\$ It's worth nothing that in MineSweeper you have an additional information that here is missing: the number of mines. \$\endgroup\$
    – edc65
    Dec 4, 2016 at 10:47
  • \$\begingroup\$ @mathmandan Yes, the input will always be rectangular with at least one cell and at least one valid solution. \$\endgroup\$
    – DJMcMayhem
    Dec 5, 2016 at 18:07

7 Answers 7


GNU Prolog, 493 bytes


An extra predicate that may be useful for testing (not part of the submission):


Prolog's definitely the right language for solving this task from the practical point of view. This program pretty much just states the rules of Minesweeper and lets GNU Prolog's constraint solver solve the problem from there.

z and i are utility functions (z does a sort of fold-like operation but on sets of three adjacent elements rather than 2; i transposes 3 lists of n elements into a list of n 3-tuples). We internally store a cell as x/y, where x is 1 for a mine and 0 for a nonmine, and y is the number of adjacent mines; c expresses this constraint on the board. r applies c to every row of the board; and so z(r,M) checks to see if M is a valid board.

Unfortunately, the input format required to make this work directly is unreasonable, so I also had to include a parser (which probably accounts for more code than the actual rules engine, and most of the time spent debugging; the Minesweeper rules engine pretty much worked first time, but the parser was full of thinkos). q converts a single cell between a character code and our x/y format. l converts one line of the board (leaving one cell that's known to be not a mine, but with an unknown number of neighbouring mines, at each edge of the line as a border); p converts the entire board (including the bottom border, but excluding the top one). All of these functions can be run either forwards or backwards, thus can both parse and pretty-print the board. (There's some annoying wiggling around with the third argument to p, which specifies the width of the board; this is because Prolog doesn't have a matrix type, and if I don't constrain the board to be rectangular, the program will go into an infinite loop trying progressively wider borders around the board.)

m is the main Minesweeper solving function. It parses the input string, generating a board with a correct border (via using the recursive case of p to convert most of the board, then calling the base case directly to generate the top border, which has the same structure as the bottom border). Then it calls z(r,[R|M]) to run the Minesweeper rules engine, which (with this call pattern) becomes a generator generating only valid boards. At this point, the board is still expressed as a set of constraints, which is potentially awkward for us; we could perhaps have a single set of constraints which could represent more than one board. Additionally, we haven't yet specified anywhere that each square contains at most one mine. As such, we need to explicitly "collapse the waveform" of each square, requiring it to be specifically either a (single) mine or a nonmine, and the easiest way to do this is to run it through the parser backwards (the var(V) on the q(63,V) case is designed to prevent the ? case running backwards, and thus deparsing the board forces it to be fully known). Finally, we return the parsed board from m; m thus becomes a generator which takes a partially unknown board and generates all the known boards consistent with it.

That's really enough to solve Minesweeper, but the question explicitly asks to check whether there's exactly one solution, rather than to find all solutions. As such, I wrote an extra predicate s which simply converts the generator m into a set, and then asserts that the set has exactly one element. This means that s will return truthy (yes) if there is indeed exactly one solution, or falsey (no) if there is more than one or less than one.

d is not part of the solution, and not included in the bytecount; it's a function for printing a list of strings as though it were a matrix, which makes it possible to inspect the boards generated by m (by default, GNU Prolog prints strings as a list of ASCII codes, because it treats the two synonymously; this format is fairly hard to read). It's useful during testing, or if you want to use m as a practical Minesweeper solver (because it uses a constraint solver, it's highly efficient).


Haskell, 193 169 168 bytes

c '?'="*!"
c x=[x]
g x|t<-x>>" ",w<-length(words x!!0)+1=1==sum[1|p<-mapM c$t++x++t,and[sum[1|m<-[-1..1],n<-[j-w,j,j+w],p!!(m+n)=='*']==read[d]|(j,d)<-zip[0..]p,d>'/']]

Usage example: g "1121 1??* 12?* 0122" -> True.

How it works: make list of all possible boards with the ? replaced by either * or ! (! means ignore later). This is done via mapM c, but before we prepend and append a bunch of spaces to the input string so that our indexing won't be out of range. For each such board check if it's a valid board by looping over all elements (index j) and if it's a number (d>'/') also over its neighbors (index n, m), count the * and compare to the number. Finally check the length of the list of valid boards.


Mathematica, 214 192 190 180 176 174 168 165 bytes


Contains U+F4A1 (Private use). This unnamed function finds all possible combinations for "?" (i.e. replacing all "?"s with "*" or 0) and checks whether there is only one valid solution.



Set b to "*".


Set q to the string "?". Check whether there is "?" in the input.

If[ ..., (x#0 ... ,0}, BlockMap[ ... ]]

If True...


Replace the first occurrence of q(="?") with b(="*") or 0 (i.e. two outputs), and apply the entire function again.

If False...


Pad the input with one layer of 0.

BlockMap[If[#[[2,2]]==b,b,Count[#,b,2]]&, ... ,{3,3},1]

Partition the input into 3 x 3 matrices with offset 1. For each partition, apply a function such that if the middle value is b(="*"), the output is the b(="*"), and if the middle value is not b(="*"), the output is the number of b(="*") in the input. This step re-evaluates all the number cells.

Cases[ ... ,#/.q->_,All]

From all of the results, find the ones that match the input

0&/@ ... =={0}

Check whether the input is length 1.


Perl, 215 bytes

213 bytes of code + -p0 flags (2 bytes).

/.*/;$c="@+";$_=A x$c."
$_".A x$c;s/^|$/A/mg;sub t{my($_)=@_;if(/\?/){for$i(0..8,"*"){t(s/\?/$i/r)}}else{$r=1;for$i(/\d/g){$r&=!/(...)[^V]{$c}(.$i.)[^V]{$c}(...)(??{"$1$2$3"=~y%*%%!=$i?"":R})/}$e+=$r}}t$_;$_=$e==1

The idea of the code is to test every possibility and see if there is one and only one that leads to a fully filled board that is valid.

More readable, the code looks like:

/.*/;$c="@+";           # count the size of a line.
$_=A x$c."\n$_".A x$c;  # add a line of "A" at the begining and another at the end.
s/^|$/A/mg;        # add a "A" at the start and the end of each line.

# The funcion that actually solves the problem
sub t { 
    my $_= pop; # Get the parameter, store it in $_ (default argument to regex).
    if (/\?/) {  # if there is another unknown char.
        for $i(0..8,"*") { # try every possibility
            t(s/\?/$i/r)  # reccursive call where the first unknown char has been replaced
    } else { # no more unknown character, so here we check if the board is valid
        $r=1;  # if r == 1 at the end, then the board is valid, otherwise it's not
        for $i (/\d/g) { # for each number present of the board
            # the following regex checks if there is a number is surrounded by either 
            # too much or too little mines.
            # (how it works: magic!)
        $e+=$r # Increment the number of valid boards.
t$_; # Call the previous function
$_=$e==1 # Checks if there is only one valid board ($_ is implicitly printed thanks to -p flag).

About the regex in the middle :


Note that [^V] just stands for "any character, including \n".
So the idea is: 3 char on a line, then 3 on the next (with $i in the middle), then 3 on the next. those 3 groups of 3 numbers are aligned, thanks to [^V]{$c} and the number we're interested into is in the middle.
And then, "$1$2$3"=~y%*%% counts the number of * (bombs) among those 9 characters: if it's different from $i, we add an empty string to match ("" => instant matching, the regex returns true), otherwise, we force a fail by trying to match R (which can't be in the string).
If the regex matches, then the board isn't valid, so we set $r to 0 with $r&=!/.../.
And that's why we add some A everywhere around each line: so we don't have to worry about edges cases of numbers that are near the edges of the board: they'll have A as neighbors, which aren't mines (of course, roughly any char could have work, I chose A).

You can run the program from the command line like that :

perl -p0E '/.*/;$c="@+";$_=A x$c."\n$_".A x$c;s/^|$/A/mg;sub t{my($_)=@_;if(/\?/){for$i(0..8,"*"){t(s/\?/$i/r)}}else{$r=1;for$i(/\d/g){$r&=!/(...)[^V]{$c}(.$i.)[^V]{$c}(...)(??{"$1$2$3"=~y%*%%!=$i?"":R})/}$e+=$r}}t$_;$_=$e==1' <<< "1121

The complexity couldn't be worst: it's O(m*9^n) where n is the number of ? on the board, and m is the number of cells on the board (without counting the complexity of the regex in the middle, which is probably pretty bad). On my machine, it works pretty fast for up to 4 ?, and starts to be slower 5, takes a few minutes for 6, and I didn't try for larger numbers.


JavaScript (ES6), 221 229


If all input is expected to be valid - that is with at least 1 solution - then I can save a byte changing s==1 with s<2

Less golfed

  a = g.match(/\?/g) || []; // array of '?' in a
  s = 1; // counter of solutions
  for(i=0; ~i;) // loop to find all configurations of ? and *
    // get next configuration
    for(i = a.length; a[--i] = '*?'[+( c = a[i] < '?')], c; );
    z = 0; // init at 0, must stay 0 if all cells count is ok
    .replace(/\?/g,c=>a[j++],j=0) // put ? and * at right places
       // look for mines in all 8 directions
       // for each mine decrease c
       // if c ends at 0, then the count is ok
       z|=c // z stays at 0 if count is ok
    )) // check neighbour count
    s-=!z // if count ok for all cells, decrement number of solutions
  return s==0 // true if exactly one solution found




<pre id=O></pre>

  • \$\begingroup\$ Op has said you can golf that byte. \$\endgroup\$ Dec 5, 2016 at 22:56
  • \$\begingroup\$ @DestructibleWatermelon thanks, I revised all and save some more bytes indeed \$\endgroup\$
    – edc65
    Dec 6, 2016 at 9:14

JavaScript (Node.js), 167 bytes

s=>g=(r=c='',[p,...q]=s,w)=>w?0:p?(g(r+0,q,p=='*')+g(r+1,q,1/p),c==1):c-=-![...s].some((p,i)=>p>' '&&[-1,1,-q,-1-q,-2-q,q,q+1,q+2].map(j=>p-=~~r[i+j])|p,q=s.search`

Try it online!

Though op say "input will always be rectangular, have at least one solution", false sample 3 doesn't match, so I still require 1 solution, not <2 solution

s=>(        // p.s. Here "block" can also mean \n
  c=0,          // possible mine count
  g=(           // recursive
    r='',       // mine states
    [p,...q]=s, // known info to check possible state for a block
    w           // invert condition, stop if true
      p?(       // for each block
        g(r+0,q,p=='*')+   // possibly not bomb if doesn't say so
        g(r+1,q,1/p),      // number/newline can't be bomb
        c==1               // only one bomb
        c-=-![...s].some(  // no block doesn't satisfy
            p>' '&& // \n don't mean number
                    // other symbols turn into NaN when counting
                    // subtract each neighbor, OOB = 0
            |p,     // difference between intended and actual
            q=s.search('\n') // how many blocks in a line
  • \$\begingroup\$ seems the "don't match" is a typo, fixing it makes it 4 solutions \$\endgroup\$
    – l4m2
    May 21, 2018 at 6:39

APL (Dyalog Unicode), 78 bytesSBCS

{1=+/∧/∘,¨{⍵[1;1]∊' *',⍕+/'*'=,⍵}⌺3 3¨(⊂⍵){' *'[⍵]@c⊢⍺}¨,⍳2⊣¨c←⍸'?'=⍵}(⊢↑⍨2⌈⍴)

Try it online!

A function that takes a 2-dimensional character matrix as its right argument, and gives 1 if the board is solvable (has unique solution) and 0 otherwise.

How it works

This is a straightforward implementation of the challenge description. Dimension extension is needed because the stencil operator isn't happy with too small arrays.

Ungolfed version:

  replace←{' *'[⍺]@coords⊢⍵}
  check←∧/∘,{⍵[1;1]∊' *',⍕+/'*'=,⍵}⌺3 3
  1=+/masks(check replace)¨⊂⍵

With comments:

sol←{  ⍝ ⍵←minesweeper board as a character matrix
  ⍝ 2D coordinates of '?'

  ⍝ possible combinations of blank as 0 and mine as 1
          2⊣¨coords  ⍝ (≢coords) copies of 2
        ,⍳           ⍝ cartesian product of 0..n-1 for each n in the above

  ⍝ create a board with selected mines
  replace←{' *'[⍺]@coords⊢⍵}
          {                }  ⍝ a function
                         ⊢⍵   ⍝ take the array ⍵
                  @coords     ⍝ replace the items at given coords
           ' *'[⍺]            ⍝ ... into blanks and mines as specified
                              ⍝ by boolean array ⍺

  ⍝ test if the complete board is valid
  check←∧/∘,{⍵[1;1]∊' *',⍕+/'*'=,⍵}⌺3 3
            {                     }⌺3 3  ⍝ for each 3x3 subarray (including ones centered at the edges)
                         ⍕+/'*'=,⍵       ⍝ s←count the mines and cast to string
             ⍵[1;1]∊' *',                ⍝ check if the center is one of blank, mine, or s
        ∧/∘,                             ⍝ reduce by AND for all subarrays

  ⍝ for all combinations, check if the board has unique solution
  1=+/masks(check replace)¨⊂⍵
      masks(      replace)¨⊂⍵  ⍝ generate all possible boards
            check              ⍝ check each board if it is a valid solution
  1=+/                         ⍝ check that there's exactly one valid solution in total

}(⊢↑⍨2⌈⍴)  ⍝ extend each dimension to have at least length 2 before calling main function
       ⍴   ⍝ dimensions
     2⌈    ⍝ max(n,2) for each dimension
  ⊢↑⍨      ⍝ extend the input to have ^ dimension; fill extended cells with blank

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.