Hanjie Puzzle Solver
Lu´ıs Cleto and Jo˜ao Marinheiro
FEUP-PLOG, Turma 3MIEIC05, Group 23
{ei11077,ei11129}@fe.up.pt
http://www.fe.up.pt
Abstract. The purpose of this project was to use constraint logic programming in Prolog to implement a solver for the 2D puzzle, Hanjie. For this purpose we used the clp(FD) library provided by SICStus Prolog
4.2.3, specifically the sum/3 and automaton/3 combinatorial constraints.
The program we developed is able to solve puzzles with dimensions up to 88x88, with only one possible solution, in less than one second. When there are multiple solutions, the execution time for the obtaining the first solution varies with the number of possible solutions.
These results show that the execution time of the program is primarily affected by the amount of possible results. While larger grid dimensions do increase the execution time, the increase is linear if the number of possible solutions is maintained. On the other hand, increasing the number of possible solutions will lead to an exponential growth in execution time. 1
Introduction
The goal of this project is to use constraint logic programming in Prolog to develop a logic program capable of solving a decision problem in the form of the 2D puzzle, Hanjie. This puzzle consists of a rectangular grid with ’clues’ on top of every column and to the left of every row that indicate the number and length of gray blocks in that column/row. To achieve this goal, first had to be chosen the data structures that would be used to represent the problem. It was decided to use lists of lists, containing the clues for each column/row in every sublist, and a similar structure for the puzzle grid where every sublist is a row of the grid. Every square of the grid can be either a 0 or a 1 where a 0 represents a blank square and 1 represents a gray square. Our implementation also allows the lists of clues to be partially or completely
References: 1. Russel, Stuart; Norvig, Peter: Artificial Intelligence: A Modern Approach. Pearson, New Jersey (2009) 2. Sterling, Lehon; Shapiro, Ehud: The Art of Prolog: Advanced Programming Techniques. The MIT Press, Massachusetts (1994) 3 5. Ullman, Jeffrey; Motwani, Rajeev; Hopcroft, John: Introduction to Automata Theory, Languages, and Computation. Addison-Wesley (2001) 6