USING BACKTRACKING TO SOLVE THE SCRAMBLE SQUARES PUZZLE



Similar documents
Genetic Algorithms and Sudoku

Section IV.1: Recursive Algorithms and Recursion Trees

Enumerating possible Sudoku grids

PARALLELIZED SUDOKU SOLVING ALGORITHM USING OpenMP

Smart Graphics: Methoden 3 Suche, Constraints

Reflection Report International Semester

Shape Dictionary YR to Y6

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Topological Treatment of Platonic, Archimedean, and Related Polyhedra

Making a Triangle Twist Foundation in EQ5

AI: A Modern Approach, Chpts. 3-4 Russell and Norvig

A Client-Server Interactive Tool for Integrated Artificial Intelligence Curriculum

PUZZLES WITH POLYHEDRA AND PERMUTATION GROUPS

A search based Sudoku solver

Confidence Intervals for the Difference Between Two Means

Mathematics of Sudoku I

Y. Xiang, Constraint Satisfaction Problems

Algorithms and Data Structures

Course Outline Department of Computing Science Faculty of Science. COMP Applied Artificial Intelligence (3,1,0) Fall 2015

DISCOVERING 3D SHAPES

Efficient Data Structures for Decision Diagrams

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Uses of Java Applets in Mathematics Education

k, then n = p2α 1 1 pα k

Binary Search Trees CMPSC 122

SOLIDS, NETS, AND CROSS SECTIONS

Introduction Solvability Rules Computer Solution Implementation. Connect Four. March 9, Connect Four

Patterns in Pascal s Triangle

Sudoku as a SAT Problem

Secret Communication through Web Pages Using Special Space Codes in HTML Files

Geometry Solve real life and mathematical problems involving angle measure, area, surface area and volume.

EFFICIENT KNOWLEDGE BASE MANAGEMENT IN DCSP

Statistical Machine Translation: IBM Models 1 and 2

PROGRESSION THROUGH CALCULATIONS FOR MULTIPLICATION

arxiv: v2 [math.ho] 4 Nov 2009

Grade 3 FCAT 2.0 Mathematics Sample Answers

Activities Grades K 2 THE FOUR-SQUARE QUILT. Put triangles together to make patterns.

Introduction to Artificial Intelligence

3.1. Solving linear equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

Ordered Lists and Binary Trees

Equations, Inequalities & Partial Fractions

Methods for Solving Sudoku Puzzles

Two-Sample T-Tests Assuming Equal Variance (Enter Means)

M. Sugumaran / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 2 (3), 2011,

Session 6 Number Theory

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Symbol Tables. Introduction

FIRST GRADE MATH Summer 2011

Integrating Benders decomposition within Constraint Programming

3D shapes. Level A. 1. Which of the following is a 3-D shape? A) Cylinder B) Octagon C) Kite. 2. What is another name for 3-D shapes?

Computer Science CS 4013/5013: Artificial Intelligence

a. Look under the menu item Introduction to see how the standards are organized by Standards, Clusters and Domains.

Algorithm Design and Analysis Homework #1 Due: 5pm, Friday, October 4, 2013 TA === Homework submission instructions ===

Level 1 - Maths Targets TARGETS. With support, I can show my work using objects or pictures 12. I can order numbers to 10 3

Sudoku puzzles and how to solve them

FACTORS, PRIME NUMBERS, H.C.F. AND L.C.M.

8 Primes and Modular Arithmetic

Data Structures Fibonacci Heaps, Amortized Analysis

MATHEMATICS: CONCEPTS, AND FOUNDATIONS Vol. III - Logic and Computer Science - Phokion G. Kolaitis

PUZZLES AND GAMES THE TOOTHPICK WAY

TREE BASIC TERMINOLOGIES

Automatic Test Data Generation for TTCN-3 using CTE

Using Peer to Peer Dynamic Querying in Grid Information Services

Lesson #13 Congruence, Symmetry and Transformations: Translations, Reflections, and Rotations

Current Standard: Mathematical Concepts and Applications Shape, Space, and Measurement- Primary

Common Data Structures

DEVELOPMENT OF A VULNERABILITY ASSESSMENT CODE FOR A PHYSICAL PROTECTION SYSTEM: SYSTEMATIC ANALYSIS OF PHYSICAL PROTECTION EFFECTIVENESS (SAPE)

Minnesota Academic Standards

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Near Optimal Solutions

COMPUTER SCIENCE (5651) Test at a Glance

Area of a triangle: The area of a triangle can be found with the following formula: in

THE APPLICATION OF ARTIFICIAL INTELLIGENCE TO SOLVE A PHYSICAL PUZZLE. Dana Cremer

Chapter 8 Geometry We will discuss following concepts in this chapter.

Integer Factorization using the Quadratic Sieve

Knowledge based system to support the design of tools for the HFQ forming process for aluminium-based products

COMPUTER SCIENCE TRIPOS

Expanding the CASEsim Framework to Facilitate Load Balancing of Social Network Simulations

Complexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar

2 When is a 2-Digit Number the Sum of the Squares of its Digits?

Persistent Binary Search Trees

The Theory of Tetris

Teaching and Learning 3-D Geometry

TA: Matt Tong Tues EBU3b B225 - Office hour WLH Discussion Thurs EBU3b B250/B250A - Lab/Office hour

Treemaps with bounded aspect ratio

Two-Sample T-Tests Allowing Unequal Variance (Enter Difference)

International Journal of Advanced Research in Computer Science and Software Engineering

Parallel Algorithm for Dense Matrix Multiplication

Pseudo code Tutorial and Exercises Teacher s Version

LAMC Beginners Circle: Parity of a Permutation Problems from Handout by Oleg Gleizer Solutions by James Newton

8 Fractals: Cantor set, Sierpinski Triangle, Koch Snowflake, fractal dimension.

Cubes and Cube Roots

ORGANIZATIONAL DESIGN AND ADAPTATION IN RESPONSE TO CRISES: THEORY AND PRACTICE

Satisfiability Checking

COMPUTER SCIENCE. Paper 1 (THEORY)

Practical Guide to the Simplex Method of Linear Programming

Transcription:

USING BACKTRACKING TO SOLVE THE SCRAMBLE SQUARES PUZZLE Keith Brandt, Kevin R. Burger, Jason Downing, Stuart Kilzer Mathematics, Computer Science, and Physics Rockhurst University, 1100 Rockhurst Road, Kansas City, MO 64110-2561 <keith.brandt@rockhurst.edu>, <kevin.burger@rockhurst.edu> ABSTRACT Constructing computer programs that solve problems (and puzzles) is central to the field of Artificial Intelligence (AI). In this paper we present an interesting puzzle and show how it can be solved using depth first search with backtracking. We feel this problem is well suited for undergraduate student projects, as it clearly demonstrates the power of backtracking when working with very large search trees. We conclude with several extensions to the original puzzle that would provide excellent research opportunities for undergraduate students. This project was supported in part by a Rockhurst University School of Arts and Sciences Dean s Summer Undergraduate Research Fellowship (Summer, 2001). INTRODUCTION Constructing computer programs that solve problems (and puzzles) is central to the field of Artificial Intelligence (AI). Work has been done in AI to find efficient and optimal search algorithms for solving various problems. In this paper we present an interesting puzzle and show how it can be solved using depth first search with backtracking. Scramble Squares is a puzzle created and marketed by b.dazzle, inc., [1]. The puzzle consists of nine squares, each of which contains four halves of various images. Figure 1 shows the nine unarranged squares of one of their puzzles. The object of the puzzle is to arrange the squares in a 3 3 grid such that the "scrambled" images are aligned, thus solving the puzzle. Similar puzzles are marketed by MindWare (Squzzle ) [2] and the DaMert Company (3D Squares ) [3]. The puzzle is very complex. Indeed, there are 4 9 9! = 95,126,814,720 different arrangements of the nine pieces. This puzzle is actually an instance of what is known in AI as a constraint satisfaction problem (CSP). These problems have been studied extensively and continue to be an important area of research. Many of them are known to belong to the complexity class NP-complete and thus are generally very

difficult to solve for large values of n. Nonetheless, there exist algorithms which are better than others for attacking CSP's. Two such algorithms include depth first search with forward checking and depth first search with backtracking. In this paper, we describe a depth first search with backtracking algorithm used to solve the puzzle, discuss the performance of the algorithm, and consider questions for further study. We feel this problem is well suited for student projects, as it clearly demonstrates the power of backtracking when working with a very large search tree. For descriptions and examples of other applications of search algorithms, see [4] and [5]. Figure 1. Unarranged Squares of the Scramble Squares Puzzle THE SETUP Squares are placed into the 3 3 grid, one at a time, in the order shown in Figure 2. Given a partial solution consisting of k squares, the remaining squares are systematically tested to see if any fit in the (k + 1)-st position. If so, the process continues. If not, the k-th square is removed (backtrack), and the other remaining squares are tested in the k-th position. Figure 2. Initial Placement of the Squares in the 3 3 Grid

The square in position 1 is not rotated, so redundant solutions that correspond to rotating the entire grid are eliminated. At each of the remaining steps, the squares being considered are tested in four different orientations (rotations). Thus, with this restriction, there are 4 8 9! = 23,781,703,680 possible arrangements of the puzzle squares. The structure of the search tree is shown in Table 1. Since the first square is not rotated, there are 9 nodes at the first level. In general the number of nodes at the k-th level of the tree is given by 9 (8 4) (7 4) ((10 k) 4) = 9! 4 k 1 / (9 k)!. There are a total of 30,563,311,978 nodes in this search tree. Table 1. Number of Nodes at Each Level of the Search Tree Level Number of nodes Root 1 1 9 2 288 3 8,064 4 193,536 5 3,890,720 6 61,931,520 7 743,178,240 8 5,945,425,920 9 23,781,703,680 THE ALGORITHM The squares are numbered 1 through 9 and are stored in an array PIECE. Orientations are numbered 0 though 3. The algorithm uses a 1 by 9 array USED of zeros and ones that keeps track of the pieces being used in the current (partial) solution. The partial solution is stored in a 2 by 9 array SOLUTION that contains the square numbers in the correct order and the orientation of each square. For example, SOLUTION = 7 5 1 9 4 3 8 2 6 0 3 1 0 0 3 1 4 2 means put PIECE(7) with orientation 0 in the first location, put PIECE(5) with orientation 3 in the second position, and so on. We use recursion to look for solutions. When we have a partial solution consisting of k squares, the procedure SOLVEPUZZLE looks for a square that will fit in the (k + 1)-st position. Figure 3 shows the basic structure of SOLVEPUZZLE.

Procedure SOLVEPUZZLE (k : integer) If k < 10 Then While there are more pieces to consider Select a new piece PIECE(n) for consideration Set ORIENTATION := 0 While ORIENTATION < 4 If new piece, in cur ORIENTATION, fits in position k Then SOLUTION(1, k) := n SOLUTION(2, k) := ORIENTATION USED(n) := 1 SOLVEPUZZLE(k + 1) USED(n) := 0 Else Increment ORIENTATION End If End While End While Else (when k = 10, we have found a solution) Display SOLUTION End If End Procedure Figure 3. The Recursive Backtracking Procedure for Solving the Puzzle RESULTS By placing counters within SOLVEPUZZLE, we can keep track of the number of times this procedure is called at each level of the search tree. These numbers are shown in Table 2. SOLVEPUZZLE was called three times at level 10, so there are three distinct solutions to this Scramble Squares puzzle. The table also reveals just how effective backtracking can be: SOLVEPUZZLE was called a total of just 672 times to search a tree with over 30 billion nodes. Table 2. Number of Recursive Calls to SOLVEPUZZLE at Level k k Calls at level k 1 1 2 9 3 37 4 140 5 80 6 203 7 62 8 116 9 21 10 3 THE JAVA APPLET We have written a Java applet that implements the puzzle [6]. The applet is initialized with a puzzle configuration corresponding to Figure 1. The user can attempt to solve the puzzle manually or can let the computer solve the puzzle using the

recursive backtracking algorithm. The applet will also create random 3 3, 4 4, and 5 5 puzzles, which can be quite challenging to solve manually. QUESTIONS FOR FURTHER STUDY The original puzzle is very interesting, but augmenting the puzzle and varying the parameters will lead to many other interesting questions. Here are some problems worth considering: 1. Are there more efficient ways to solve the Scramble Squares puzzle? References [2] and [3] describe several other search techniques that might be used to solve the puzzle. 2. By rearranging the images on the squares, is it possible to construct a puzzle that has a unique solution? 3. Construct and solve a similar puzzle for a 4 4 grid. This puzzle would allow over 22 10 different arrangements of the squares. How long would it take to solve a 5 5 puzzle? 4. Construct and solve a two-sided puzzle (whose pieces can be flipped over as well as rotated). 5. Construct and solve a similar puzzle consisting of six squares that must be arranged on the faces of a cube. Are any six of the existing nine Scramble Squares pieces a solution to such a puzzle? 6. Construct and solve similar puzzles where equilateral triangles are arranged on the faces of an octahedron (or, for the ambitious, an icosahedron) or where pentagons are arranged on the faces of a dodecahedron. ACKNOWLEDGEMENTS We would like to express our appreciation to the Rockhurst University Arts and Sciences Dean's Office for its support of this undergraduate research project. REFERENCES 1. Scramble Squares, 2001. [online]. b-dazzle, inc., [cited 6 Nov 2001]. <www.bdazzle.com>. Scramble Squares is a registered trademark of b-dazzle, inc. 2. Squzzle, 2000. [online]. MindWare, [cited 7 Jan 2002]. <www.mindwareonline. com>. Squzzle is a trademark of MindWare. 3. 3D Squares, 2001. [online]. DaMert Company, [cited 7 Jan 2002]. <www. damert.com/pages/3dsquares.html>. 3D Squares is a trademark of the DaMert Company. 4. Michalewiz, Z., Fogel, D. B., 2000. How to Solve It: Modern Heuristics, Springer- Verlag, Berlin, Germany. 5. Russell, Stuart J., Norvig, Peter, 1995. Artificial Intelligence: A Modern Approach, Prentice-Hall, Inc., Englewood Cliffs, New Jersey. 6. Burger, Kevin R., 2001. [online]. Rockhurst University, [cited 7 Jan 2002]. <cte. rockhurst.edu/burgerk/research/scramble.html>.