Miscellaneous Problems

Problem 90

intermediate

Eight queens problem.

This is a classical problem in computer science. The objective is to place eight queens on a chessboard so that no two queens are attacking each other; i.e., no two queens are in the same row, the same column, or on the same diagonal.

Hint

Represent the positions of the queens as a list of numbers 1 ... N. Example: [4, 2, 7, 3, 6, 8, 5, 1] means that the queen in the first column is in row 4, the queen in the second column is in row 2, etc. Use the generate-and-test paradigm.

Problem 91

intermediate

Knight's tour.

Another famous problem is this one: How can a knight jump on an chessboard in such a way that it visits every square exactly once.

Hints:

  • Represent the squares by pairs of their coordinates of the form , where both and are integers between and . (Note that '' is just a convenient functor, not division!)
  • Define the relation to express the fact that a knight can jump from to on a chessboard.
  • And finally, represent the solution of our problem as a list of knight positions (the knight's tour).

Problem 92

hard

Von Koch's conjecture.

Several years ago I met a mathematician who was intrigued by a problem for which he didn't know a solution. His name was Von Koch, and I don't know whether the problem has been solved since. Anyway the puzzle goes like this: Given a tree with nodes (and hence edges). Find a way to enumerate the nodes from to and, accordingly, the edges from 1 to in such a way, that for each edge the difference of its node numbers equals to .

The conjecture is that this is always possible. For small trees the problem is easy to solve by hand. However, for larger trees, and 14 is already very large, it is extremely difficult to find a solution. And remember, we don't know for sure whether there is always a solution.

Write a function that calculates a numbering scheme for a given tree. What is the solution for the larger tree pictured above?

Problem 93

hard

An arithmetic puzzle.

Given a list of integer numbers, find a correct way of inserting arithmetic signs (operators) such that the result is a correct equation.

Example

With the list of numbers [2, 3, 5, 7, 11] we can form the equations or (and ten others!).

Problem 94

hard

Generate -regular simple graphs with nodes.

In a -regular graph all nodes have a degree of ; i.e. the number of edges incident in each node is .

How many (non-isomorphic!) 3-regular graphs with 6 nodes are there?

Problem 95

intermediate

English number word.

On financial documents, like cheques, numbers must sometimes be written in full words. Example: 175 must be written as one-seven-five.

Write a function full_words/1 to print (non-negative) integer numbers in full words.

Problem 96

intermediate

Syntax checker (alternative solution with difference lists).

In a certain programming language (Ada) identifiers are defined by the syntax diagram (railroad chart) opposite. Transform the syntax diagram into a system of syntax diagrams which do not contain loops; i.e. which are purely recursive.

Using these modified diagrams, write a function identifier/1 that can check whether or not a given string is a legal identifier.

identifier(Str) == true iff Str is a legal identifier

Problem 97

intermediate

Sudoku.

Sudoku puzzles go like this.

Problem statement

..48...17
67.9.....
5.8.3...4
---------
3..74.1..
.69...78.
..1.69..5
---------
1...8.3.6
.....6.91
24...15..

Solution

934825617
672914853
518637924
---------
325748169
469153782
781269435
---------
193582346
853476291
246391578

Every spot in the puzzle belongs to a (horizontal) row and a (vertical) column, as well as to one single 3x3 square (which we call "square" for short). At the beginning, some of the spots carry a single-digit number between 1 and 9.

The problem is to fill the missing spots (denoted .) with digits in such a way that every number between 1 and 9 appears exactly once in each row, in each column, and in each square.

Problem 98

hard

Nonogram.

Around 1994, a certain kind of puzzles was very popular in England. The "Sunday Telegraph" newspaper wrote: "Nonograms are puzzles from Japan and are currently published each week only in The Sunday Telegraph. Simply use your logic and skill to complete the grid and reveal a picture or diagram." As a Prolog programmer, you are in a better situation: you can have your computer do the work! Just write a little program ;-). The puzzle goes like this: Essentially, each row and column of a rectangular bitmap is annotated with the respective lengths of its distinct strings of occupied cells. The person who solves the puzzle must complete the bitmap given only these lengths. Problem statement: Solution:

||||||||| 3 ||X|X|X||||| 3
|
|||||||| 2 1 |X|X||X||||| 2 1
||||||||| 3 2 ||X|X|X|||X|X| 3 2
|
|||||||| 2 2 |||X|X|||X|X| 2 2
|
|||||||| 6 |||X|X|X|X|X|X| 6
|
|||||||| 1 5 |X||X|X|X|X|X|| 1 5
|
|||||||| 6 |X|X|X|X|X|X||| 6
|
|||||||| 1 |||||X|||| 1
||||||||| 2 ||||X|X|||| 2 . 1 3 1 7 5 3 4 3 1 3 1 7 5 3 4 3 . 2 1 5 1 2 1 5 1 . For the example above, the problem can be stated as the two lists [[3],[2,1],[3,2],[2,2],[6],[1,5],[6],[1],[2]] and [[1,2],[3,1],[1,5],[7,1],[5],[3],[4],[3]] which give the "solid" lengths of the rows and columns, top-to-bottom and left-to-right, respectively. Published puzzles are larger than this example, e.g. 25 x 20, and apparently always have unique solutions.

Problem 99

hard

Crossword puzzle.

Given an empty (or almost empty) framework of a crossword puzzle and a set of words. The problem is to place the words into the framework. The particular crossword puzzle is specified in a text file which first lists the words (one word per line) in an arbitrary order. Then, after an empty line, the crossword framework is defined. In this framework specification, an empty character location is represented by a dot (.). In order to make the solution easier, character locations can also contain predefined character values. The puzzle opposite is defined in the file p99a.dat, other examples are p99b.dat and p99d.dat. There is also an example of a puzzle (p99c.dat) which does not have a solution. Words are strings (character lists) of at least two characters. A horizontal or vertical sequence of character places in the crossword puzzle framework is called a site. Our problem is to find a compatible way of placing words onto sites. Hints: (1) The problem is not easy. You will need some time to thoroughly understand it. So, don't give up too early! And remember that the objective is a clean solution, not just a quick-and-dirty hack! (2) Reading the data file is a tricky problem for which a solution is provided in the file p99-readfile.pl. Use the function read_lines/2. (3) For efficiency reasons it is important, at least for larger puzzles, to sort the words and the sites in a particular order. For this part of the problem, the solution of P28 may be very helpful.