Implement a state-space search

Assignment Help Data Structure & Algorithms
Reference no: EM13749398

You will implement a state-space search that will find a solution to the sixteenpuzzle. For this program, in addition to the state-space search control, you will need to implement at least two other classes. One class will be a Queue class. This is not a difficult class to implement if you make use of the LinkedList collection class. For example, the add() method appends the parameter to the end of the list, and the remove() method removes and returns the object at the head of the list: hence you have a queue. There is also a size() method in the class, which makes it easy to implement an isEmpty() method for the Queue class.

The second class will be a State class. As in the example, a state should consist of a one dimensional, 16 location, integer array and another integer variable that holds the index of the space. In addition, since we want to print out the moves that solve the problem, we need something to hold the moves thus far. A String will work quite well, since the moves can be represented by the letters "U", "D", "L", "R". As a move is made, just concatenate the appropriate letter to the end of the String. And, to read the moves for a solution, the charAt() method in the String class can get to individual letters.

Your class should have accessor methods that return the current values of the array, the space variable and the moves variable, as well as a constructor that takes all three of these values. This will allow a new state to be created??*?t is a clone of the parent state. Then there should be four methods that implement a move. When a move is implemented, the array and the value of the space index are changed and the appropriate letter is concatenated to end of the move list. In addition, there should be four boolean methods that test if a move can be applied to a state.

The tests to see if a move applies to a state are given in the 16-puzzle example. However, there should be one other condition added to the tests. If, for example, I am checking to see if a Down rule applies to a state, a condition that must be true is that the space index is less than 12. In addition, the last move in the move list should not be a "U". Why? Think of the 16-puzzle game: if you move a tile up, one move you can always make next is to move that tile down. But, this just brings you back to the previous situation and is a wasted move. So, a move, X, should not be applicable to a state if the last entry in the move list is the opposite of X. Does this condition make a difference? Well, I tried the basic rules without this condition on a puzzle that would take about 10 moves to solve. I also had the program keep track of the number of new states created and print out this number each time a new state was created. The program could not solve the puzzle because it ran out of heap space after creating almost 800,000 states. I then added this condition to the rules, and the program was able to solve the puzzle. It had made less than 5,000 new states by the time the puzzle was solved.

Finally, there should be two other methods in the State class. One is a boolean method that tests is the state is a goal state or not. The other method should print the moves of the state.

The main program should have a means of taking a list of numbers and creating an initial state. For example, the list 4, 0, 2, 3, 5, 1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 represents an initial state of:

4

 

2

3

5

1

6

7

8

9

10

11

12

13

14

15

As the initial state, the move list for this state should be the empty string. Once the initial state is created, it is placed into the queue. Then a loop, while the queue is not empty and the puzzle is not solved, is started. Within the loop, take a state out the queue and check if each of the four moves apply. If a move applies, create a new state that is a clone of the current state, apply the move to the new state, and check if the new state is the goal state. If it is the goal state, set a global state variable to this state and set the loop condition to solved. If it is not the goal state, add it to the queue.

The loop should end for one of two reasons. If the queue is empty, then no solution is found. If the solution is found, then a global variable should have been set with the solution state. The moves of this state are then printed out. For example, in the puzzle above, three moves solve this puzzle. They are Down, Left, and Up (remember these moves apply to the space).

For testing purposes, here are four more initial states along with the set of moves that solve the puzzle. Remember that the 0 represents the location of the space.

1. 4, 1, 2, 3, 5, 6, 10, 7, 8, 0, 9, 11, 12, 13, 14, 15. Solution: R, U, L, L, U
2. 1, 2, 6, 3, 4, 0, 10, 7, 8, 5, 9, 11, 12, 13, 14, 15. Solution: D, R, U, U, L, L
3. 4, 1, 2, 3, 8, 0, 5, 7, 9, 10, 6, 11, 12, 13, 14, 15. Solution: R, D, L, L, U, U
4. 1, 2, 6, 3, 4, 5, 0, 7, 12, 8, 10, 11, 13, 14, 9, 15. Solution: D, D, L, L, U, R, R, U, U, L, L.

Reference no: EM13749398

Questions Cloud

Explain how governments restrict international trade : Explain how governments restrict international trade and who benefits as well as who loses from the restrictions - Cisco and other major corporations close down their American operations and move to Africa?
What do believe are the two biggest social responsibility : What do you believe are the two biggest social responsibility issues companies should be addressing today?  Why are they the two most important? (Cite examples from outside research from the news and from experiences at work.)How would addressing the..
Describe the manufacturing process : Choose a product to manufacture and describe the manufacturing process. Prepare the following budgets for 1 quarter, broken down monthly, regarding your chosen item:
Specific quality program tactics were involved : Using personal experience in regard to the quality improvement programs that you discussed in the previous week, which of the following specific quality program tactics were involved
Implement a state-space search : You will implement a state-space search that will find a solution to the sixteenpuzzle. For this program, in addition to the state-space search control, you will need to implement at least two other classes
Discretion of lower courts : * From the e-Activity, describe at least three (3) factors that you believe permit the lower court to implement decisions at their discretion based on your reading of Chapter 14. Provide a rationale for your response.
How do you know that the maps shows the united states : How do you know that the maps shows the United States after 1883 not before.
Focusing more today on social responsibility : There are many examples of how the actions of a company have negatively affected consumers. Product recalls, bans, and warning labels have helped to protect consumers and companies are focusing more today on social responsibility. Examine why has ..
What are some obstacles would d.r. horton might face : Suppose the U.S. Government removed tariffs in your industry. What impact would that have on D.R. Horton? Explain thoroughly and what are some obstacles would D.R. Horton might face with production in another country?

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Kind of switching to configure switch to use

Your network's traffic load is very high all times, day and night. What kind of switching do you configure switch to use?

  Determining worst-case time complexity

The recent discovery of the following fragment of uncommented procedural C code in the Sunlab has caused a big scandal. What is the worst-case time complexity of foo(a,1,N,k), and for which inputsdoes it occur?

  What is minimum number of nodes expanded for bfs and dfs

Consider the following graph representing the state space and operators of a navigation problem: What is the minimum number of nodes expanded and the storage needed for BFS and DFS?

  .specify and define a method for linkedbag

Add a constructor to the class LinkedBag that creates a bag from a given array of entries.Specify and define a method for LinkedBag that removes a random entry from the bag.

  Powerpoint presentation with the focus on stress management

Assume you have been asked to help new students identify ways in which they can manage their time so that they can be successful in an online learning environment.

  Draw a red-black tree

Draw a red-black tree for the following values inserted in this order. Illustrate each operation that occurs: w k o s y t p r

  Difference between sequential, random and binary file access

Discuss the difference between sequential file access, random file access, and binary file access? For each of the three types, provide an example of an application where the use of one type is better than the other 2-types.

  Exploring oop and its data structures

Exploring OOP and its Data Structures

  Data structures and algorithms

Provides learners with an understanding of how data structures are used in algorithms and enables them to design and implement data structures

  Part-11 suppose you want to demonstrate an erd to someone

part-11. suppose you want to demonstrate an erd to someone who has never seen one. provide a scenario from everyday

  Write a program that checks if left and right braces

Write a program that checks if left and right braces, brackets, and parentheses are balanced

  Develop a solution for the problem and mention algorithms

Spaces between tokens are allowed but not required. The program will convert the (user input) infix expression to postfix (RPN) form and display the converted expression on the screen.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd