Information Technology Reference
In-Depth Information
Figure 5.3.
A4
×
4 Sudoku problem
×
You are given a 4
4 grid, where some of the cells are blank and
some contain numbers between 1 and 4. Fill in all the blank cells
with numbers between 1 and 4 so that
the numbers in each row (1, 2, 3, 4) are unique,
NW NE
SW
SE
the numbers in each column (1, 2, 3, 4) are unique,
the numbers in each 2
×
2 quadrant (NW, NE, SW, SE) are
unique.
5.2 A first example: Sudoku
×
Figure 5.3 shows a 4
4 Sudoku problem. It is a simpler version of Sudoku than the
×
9
9 version that appears in newspapers and puzzle topics. (Section 5.2.3 discusses
the 9
×
9 version.)
5.2.1 The anonymous variable in Prolog
A certain feature of Prolog, the anonymous variable , is useful for solving the Sudoku
problem. Prolog allows the underscore character _ to be used as a variable in those
cases where the goal is not finding the value of the variable but ensuring that there is
one:
?- child(X,sue).
% Who is a child of Sue?
X=john
Yes
?- child(_,sue).
% Does Sue have a child?
Yes
?- child(_,john).
% Does John have a child?
No
?- child(_,_).
% Does anyone have a child?
Yes
Note that each occurrence of the anonymous variable can be for a different value. So
the last query behaves like child(X,Y) , rather than like child(X,X) .
 
Search WWH ::




Custom Search