In this article about creating a Minesweeper solver, the author talks about using matrices to solve given portions of a Minesweeper board. While reading that, I thought of a different way to limit the domain to {0,1}: instead of using substitutions after RowReduce, use Boolean algebra, specifically counting functions. But here I run into a problem: using SatisfiabilityInstances gives one or more solutions, but unless if all of the instances are compared, I (and the code) wouldn't know which of the hundreds or thousands of variables have the same value in all instances, i.e. are guaranteed. (One does not want to make unnecessary guesses in Minesweeper!) Is there some way to get the complete solution domain (e.g. {a,b,c}->{True, False, Unknown}) without going through all of the possibilities?

EDIT: Here is a more specific example. Start with this Minesweeper layout:


(The #s indicate nonspecific squares assumed by the numbers to be non-mines. If they do contain marked mines, adjust the numbers accordingly. This won't really matter that much for this example, though.)

The corresponding matrix (formatted according to the link) is this:

[1 1 1 0 0 0 0 | 2]
[0 1 1 1 1 1 0 | 2]
[0 0 0 0 1 1 1 | 2]

which row-reduces to:

[1  0  0 -1  0  0  1 | 2]
[0  1  1  1  0  0 -1 | 0]
[0  0  0  0  1  1  1 | 2]

The first row indicates that a and g are mines, and that d isn't. However, the code doesn't know that without the restriction to the domain of {0,1}, which would be a suitable alternative, but I don't know how to implement that.

(Incidentally, which RowReduce method would be best for this application, if the domain problem is solved? The documentation doesn't go into the specifics of what each method does.)

The corresponding Boolean function would be something along the lines of this:

And[BooleanCountingFunction[{2}, 3][a, b, c],
    BooleanCountingFunction[{2}, 5][b, c, d, e, f],
    BooleanCountingFunction[{2}, 3][e, f, g]]

What I was wanting was a function boolsolve[eqns,vars] to output something along the lines of {a->True,d->False,g->True} in this particular instance, and similarly for other starting positions, once they're marked suitably; I feel like I might be able to handle the marking portion of the code. (After reconsideration, I realized that the Unknowns in the results would just get in the way.)

(Said function could also operate off of just a list of the counting functions; one just needs to substitute in Apply[And,list] for the equation above.)

  • $\begingroup$ There isn't really enough code or description here to determine what it is you are trying to do. It would be good to have in this post a smallish example of the input and desired output, extracted from the Minesweeper world and just translated into text and code. $\endgroup$ Apr 4, 2016 at 22:51
  • $\begingroup$ @DanielLichtblau Added an example, and realized that this problem might just come down to a domain restriction after a RowReduce. $\endgroup$ Apr 5, 2016 at 5:25
  • $\begingroup$ Re formatting, it is better to use cut-and-pastable Mathematica InputForm. $\endgroup$ Apr 5, 2016 at 14:42

1 Answer 1


One approach is to place inequality and domain restrictions in Solve. I show this below, using the example in the post.

mat = {{1, 1, 1, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 1, 
    1, 1}};
vars = Array[x, Length[mat[[1]]]];
rhs = {2, 2, 2};
Solve[Join[Thread[mat.vars == rhs], 
  Thread[0 <= vars <= 1]], vars, Integers]

(* Out[261]= {{x[1] -> 1, x[2] -> 0, x[3] -> 1, x[4] -> 0, x[5] -> 0, 
  x[6] -> 1, x[7] -> 1}, {x[1] -> 1, x[2] -> 0, x[3] -> 1, x[4] -> 0, 
  x[5] -> 1, x[6] -> 0, x[7] -> 1}, {x[1] -> 1, x[2] -> 1, x[3] -> 0, 
  x[4] -> 0, x[5] -> 0, x[6] -> 1, x[7] -> 1}, {x[1] -> 1, x[2] -> 1, 
  x[3] -> 0, x[4] -> 0, x[5] -> 1, x[6] -> 0, x[7] -> 1}} *)
  • $\begingroup$ No, this is not what I'm looking for, as it displays all of the results, like with SatisfiabilityInstances; this would easily be resolved by looking through all of the possibilities and figuring out which variables have the same values over all of them. I'm looking more for something like SolveAlways, if it included an option for a choice of domain like Solve does. (SolveAlways apparently doesn't look through all possibilities, as there are infinitely many possibilities in any non-discrete domain.) Or is there no better way to do such a thing, since Minesweeper is NP? $\endgroup$ Apr 5, 2016 at 22:38
  • $\begingroup$ You can iterate FindInstance with similar conditions and alternately setting a specific variable to 0 or 1. Any time you get no solution for one case you have forced that variable to take on the other value. Any time both give solutions it gets "unknown". $\endgroup$ Apr 5, 2016 at 23:33
  • $\begingroup$ Eh, I guess that works. Would have liked to see something more elegant, but I'll take what I can get. $\endgroup$ Apr 6, 2016 at 3:02

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.