puzzle

© All Rights Reserved

3 views

puzzle

© All Rights Reserved

- Ieee Xtreme 6.0 Sample Questions
- Security Issues in Drinking Water Distribution
- Artificial Intelligence Brief Course Summary
- Pugh Matrix
- 1611.13-9205
- Essentials of the AStar Algorithm
- Ya Mi Let
- v113n10p775
- A design procedure for a supervisory control structure in plantwide control_Marcela_Moscoso_2014.pdf
- A. Shukla Advanced Three Stage Pseudo Inspired Weight Improved Crazy Particle Swarm Optimization for Unit Commitment Problem
- Combining Static and Dynamic Models
- A Simple Multi-objective Optimization Problem
- CIRP_Paper_Simulation-based Twist Drill Design and Geometry Optimization
- Syllabi
- UpliftCapacity
- Sensitivity Analysis of a Mixed Integer Linear Programming Model for Optimal Hydrothermal Energy Generation for Ghana
- Sprengel Ford
- NEUTROSOPHIC MAGDM BASED ON ENTROPIES OF DEGREES α, β AND RNORM
- 1-s2.0-S0263876212003012-main
- Multi Objective Particle Swarm Optimization for Size and Shape Optimization of 2D Truss Structures

You are on page 1of 3

8 Puzzle

Write a program to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm.

The problem. The 8-puzzle problem is a puzzle popularized by Sam Loyd in the 1870s. It is played on a 3-by-3 grid with 8

square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order. You are

permitted to slide blocks horizontally or vertically into the blank square. The following shows a sequence of legal moves

from an initial board position (left) to the goal position (right).

1 3 1 3 1 2 3 1 2 3 1 2 3

4 2 5 => 4 2 5 => 4 5 => 4 5 => 4 5 6

7 8 6 7 8 6 7 8 6 7 8 6 7 8

initial goal

Best-first search. We now describe an algorithmic solution to the problem that illustrates a general artificial intelligence

methodology known as the A* search algorithm. We define a state of the game to be the board position, the number of

moves made to reach the board position, and the previous state. First, insert the initial state (the initial board, 0 moves, and a

null previous state) into a priority queue. Then, delete from the priority queue the state with the minimum priority, and insert

onto the priority queue all neighboring states (those that can be reached in one move). Repeat this procedure until the state

dequeued is the goal state. The success of this approach hinges on the choice of priority function for a state. We consider

two priority functions:

Hamming priority function. The number of blocks in the wrong position, plus the number of moves made so far to get

to the state. Intutively, states with a small number of blocks in the wrong position are close to the goal state, and we

prefer states that have been reached using a small number of moves.

Manhattan priority function. The sum of the distances (sum of the vertical and horizontal distance) from the blocks to

their goal positions, plus the number of moves made so far to get to the state.

For example, the Hamming and Manhattan priorities of the initial state below are 5 and 10, respectively.

8 1 3 1 2 3 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8

4 2 4 5 6 ---------------------- ----------------------

7 6 5 7 8 1 1 0 0 1 1 0 1 1 2 0 0 2 2 0 3

We make a key oberservation: to solve the puzzle from a given state on the priority queue, the total number of moves we

need to make (including those already made) is at least its priority, using either the Hamming or Manhattan priority

function. (For Hamming priority, this is true because each block out of place must move at least once to reach its goal

position. For Manhattan priority, this is true because each block must move its Manhattan distance from its goal position.

Note that we do not count the blank tile when computing the Hamming or Manhattan priorities.)

Consequently, as soon as we dequeue a state, we have not only discovered a sequence of moves from the initial board to the

board associated with the state, but one that makes the fewest number of moves. (Challenge for the mathematically inclined:

prove this fact.)

A critical optimization. After implementing best-first search, you will notice one annoying feature: states corresponding to

the same board position are enqueued on the priority queue many times. To prevent unnecessary exploration of useless

states, when considering the neighbors of a state, don't enqueue the neighbor if its board position is the same as the previous

state.

8 1 3 8 1 3 8 1 3

4 2 4 2 4 2

7 6 5 7 6 5 7 6 5

Your task. Write a program Solver.java that reads the initial board from standard input and prints to standard output a

sequence of board positions that solves the puzzle in the fewest number of moves. Also print out the total number of moves

and the total number of states ever enqueued.

https://introcs.cs.princeton.edu/java/assignments/8puzzle.html 1/3

23/11/2017 8-Puzzle Programming Assignment

The input will consist of the board dimension N followed by the N-by-N initial board position. The input format uses 0 to

represent the blank square. As an example,

% more puzzle04.txt

3

0 1 3

4 2 5

7 8 6

1 3

4 2 5

7 8 6

1 3

4 2 5

7 8 6

1 2 3

4 5

7 8 6

1 2 3

4 5

7 8 6

1 2 3

4 5 6

7 8

Number of moves = 4

Note that your program should work for arbitrary N-by-N boards (for any N greater than 1), even if it is too slow to solve

some of them in a reasonable amount of time.

Detecting infeasible puzzles. Not all initial board positions can lead to the goal state. Modify your program to detect and

report such situations.

% more puzzle-impossible3x3.txt

3

1 2 3

4 5 6

8 7 0

No solution possible

Hint: use the fact that board positions are divided into two equivalence classes wirth repsect to reachability: (i) those that

lead to the goal position and (ii) those that lead to the goal position if we modify the initial board by swapping any pair of

adjacent (non-blank) blocks. There are two ways to apply the hint:

Run the A* algorithm simultaneously on two puzzle instances - one with the initial board and one with the initial

board modified by swapping a pair of adjacent (non-blank) blocks. Exactly one of the two will lead to the goal

position.

Derive a mathematical formula that tells you whether a board is solvable or not.

Deliverables. Organize your program in an appropriate number of data types. At a minimum, you are required to implement

the following APIs. Though, you are free to add additional methods or data types (such as State).

public Board(int[][] tiles) // construct a board from an N-by-N array of tiles

public int hamming() // return number of blocks out of place

public int manhattan() // return sum of Manhattan distances between blocks and goal

public boolean equals(Object y) // does this board equal y

public Iterable<Board> neighbors() // return an Iterable of all neighboring board positions

public String toString() // return a string representation of the board

// test client

public static void main(String[] args)

}

public Solver(Board initial) // find a solution to the initial board

https://introcs.cs.princeton.edu/java/assignments/8puzzle.html 2/3

23/11/2017 8-Puzzle Programming Assignment

public boolean isSolvable() // is the initial board solvable?

public int moves() // return min number of moves to solve the initial board;

// -1 if no such solution

public String toString() // return string representation of solution (as described above)

// read puzzle instance from stdin and print solution to stdout (in format above)

public static void main(String[] args)

}

Submit Board.java, Solver.java (with the Manhattan priority) and any other helper data types that you use (excluding

those in stdlib.jar and adt.jar). Finally, submit a readme.txt file and answer the questions.

https://introcs.cs.princeton.edu/java/assignments/8puzzle.html 3/3

- Ieee Xtreme 6.0 Sample QuestionsUploaded byRocky Jones
- Security Issues in Drinking Water DistributionUploaded byYoussef
- Artificial Intelligence Brief Course SummaryUploaded byFlamingstar Production
- Pugh MatrixUploaded bySulaiman Khan
- 1611.13-9205Uploaded byonigas
- Essentials of the AStar AlgorithmUploaded byFredrik Ulvin
- Ya Mi LetUploaded byJulio Ortega Revilla
- v113n10p775Uploaded byhehusa25
- A design procedure for a supervisory control structure in plantwide control_Marcela_Moscoso_2014.pdfUploaded byAndrés Ceverisae
- A. Shukla Advanced Three Stage Pseudo Inspired Weight Improved Crazy Particle Swarm Optimization for Unit Commitment ProblemUploaded byFlashNArrow
- Combining Static and Dynamic ModelsUploaded byTrần Hữu Duy
- A Simple Multi-objective Optimization ProblemUploaded bychamp1909
- CIRP_Paper_Simulation-based Twist Drill Design and Geometry OptimizationUploaded byalmedin_hecimov8494
- SyllabiUploaded bySaranya Thangaraj
- UpliftCapacityUploaded byGanesh Ravi
- Sensitivity Analysis of a Mixed Integer Linear Programming Model for Optimal Hydrothermal Energy Generation for GhanaUploaded byIJSTR Research Publication
- Sprengel FordUploaded bylumas
- NEUTROSOPHIC MAGDM BASED ON ENTROPIES OF DEGREES α, β AND RNORMUploaded byMia Amalia
- 1-s2.0-S0263876212003012-mainUploaded byMossa Laidani
- Multi Objective Particle Swarm Optimization for Size and Shape Optimization of 2D Truss StructuresUploaded byindrabudhi
- FOM Simple SettingUploaded byKevin Johnson Gebauer
- AI QBankUploaded byGnanaprakasam
- Optimization of Flexural Prediction for Ribbed Floors in Bending, Shear and DeflectionUploaded byAJER JOURNAL
- lin1992.pdfUploaded byMaz Har Ul
- cl3Uploaded byIsmail Ekmekci
- Computer Controlled SystemsUploaded byJuan Antonio Monaco Nuñes
- The Numerical Solution of Variational PrUploaded byLen Dierickx
- LIFETIME MAXIMIZATION AND RESOURCE MANAGEMENT IN WIRELESS SENSOR NETWORKSUploaded byfarhad1356
- fulltextUploaded bychadson04
- A Novel Approach for Optimum Allocation of Flexible AC Transmission Systems Using Harmony Search TechniqueUploaded byDavid Perez

- 39582369-JavaUploaded byhypnozombie
- MAST20005 Statistics Assignment 1Uploaded byAnonymous na314kKjOA
- Planets and Asteroids in the gamma Cephei SystemUploaded byardeegee
- Syllabus Cse RuetUploaded byapi-3817242
- Complete 8086 Instruction SetUploaded byEduardo Luis Ipanaqué
- StatisticsUploaded byOluwaseyi Olujimi Omotere
- Ee 341 Course NotesUploaded byMarvZz Villasis
- QuinolinasUploaded byAndhy Ramirez MauriCio
- NorthStar Red Batteries (SES-541-56)Uploaded byNicolas Prieto Medina
- {\Rtf1\Adeflang1025\Ansi\Ansicpg1252\Uc1\Adeff31507\Deff0\Stshfdbch31505\Stshfloch 31506\Stshfhich31506\Stshfbi0\Deflang1033\Deflangfe1033\Themelang1033\Themelangfe0 \Themelangcs0{\Fonttbl{\f0\Fbidi \Froman\Fcharset0\Fprq2{\*\Panose 02020603050405020304}Times New Roman;}{\f34\FbidiUploaded byMatthew Alvarez
- Statistics (1)Uploaded byAngela Bomediano
- 56 Windows dornier 328Uploaded bymiguel ayala
- rttestUploaded byrlynmndz
- Cse III Logic Design 10cs33 NotesUploaded bySahil Qaiser
- Slides 1122Uploaded byvaishnu dass
- Ten Commandments ship resistanceUploaded bynautapronauta
- En OU 8.1.5 Ref BookUploaded byFranco Medrano
- Speed GovernorUploaded bymanish_chaturvedi_6
- Marc2005_ReleaseGuideUploaded byMahmud Kori Effendi
- Moeller-DF5-Manual.pdfUploaded byspiritos123456789
- Dresser-Rand GFC Databook_ Gas Field Policies-2Uploaded bykistai
- Quitosana 1Uploaded byRicardo Zara
- Maltol complexesUploaded bywaherzog
- AE 305 HW-4 (final)Uploaded byDurukan Tamkan
- Heat Radiation.Uploaded byBiprodeep14
- CESH HowMesureStress MBUploaded byMuhammad Umer Iftikhar
- (Brown and Churchill) James Brown, Ruel Churchill-Complex Variables and Applications-McGraw-Hill Education (2013)Uploaded byJuan Van Halen
- Kiswire - NeptuneUploaded byalphading
- Bridge BalancingUploaded byettypasewang
- Sparse signal recovery in a transform domain.pdfUploaded bychokbing