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

  Explaining view of header and footer areas of worksheet

In which view can you see header and footer areas of worksheet?

  2n-1 comparisons are necessary in the worst case

Prove that 2n-1 comparisons are necessary in the worst case to merge two sorted lists containing n elements each.

  Instance of the single source shortest paths

instance of the single source shortest paths problem with vertex a as the source

  Question about edge connectivity

The edge connectivity of an indirected graph is minimum number k of edges that must be removed to disconnect the graph.

  Write an algorithm that converts a decimal number

Write an algorithm that can be used to calculate the commission earned in a real estate transaction.  The chart below describes the formulas used to calculate the commission.

  Equation apply boolean algebra

Using this equation apply boolean algebra in order to prove the commutative and associative properties for binary addition: x(+)y=y(+)x  (x(+)y)(+)z=x(+)(y(+)z)

  Propose an efficient data structure

Propose an efficient data structure that may hold the tour operator's data using a normalization process. Describe each step of the process that will enable you to have a 2nd Normal Form data structure.

  Calculate worst-case run-time complexity of algorithm

Calculate the worst-case run-time complexity of your algorithm and prove optimality of the solution it gives. Suppose that the road is a straight line with a western end and an eastern end.

  Determine minimum number of total nodes tree can have

If binary tree has height 4, determine minimum number of total nodes tree can have? c. If binary tree has height 4, determine the maximum number of total nodes tree can have?

  Show result of inserting keys using quadratic probing

Show the result of inserting these keys by using linear probing, using quadratic probing with c1 = 1and c2 = 3, and using double hashing with h2(k) = 1 + (k mod (m-1)).

  Formula to compute number of address bus conductors

If an address bus needs to be able to address 8-devices, how many conductors will be needed? What if each of those devices also requires to be able to talk back to the I/O control device?

  Problems on edges and graphs

Suppose if we add an edge to a biconnected graph with k strongly connected components, then there are 3-situations: the endpoints of edge lie in different strongly connected component and there is no path between 2 in the original graph,

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