View the step-by-step solution to:

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

Computer science assignment.

Puzzle that is solved by DFS or BFS, has to be in O(n^2) time.

Thanks

All required documents are attached.

4 0 5 2 6 1 7 3 8 0 2 7 6 5 4 3 1 8 3 1 4 7 6 5 0 8 2 7 2 8 3 4 6 0 1 5 6 2 0 4 7 8 5 1 3 2 5 1 4 7 6 3 0 8 2 1 8 3 5 7 0 6 4 3 5 7 2 1 4 0 6 8 5 2 8 0 7 3 4 1 6 7 1 0 3 8 5 2 6 4
1 3 8 5 6 0 4 2 7 2 8 4 6 1 3 7 0 5 2 4 8 5 1 7 6 0 3 2 5 3 4 7 6 1 0 8 7 6 2 1 0 3 8 5 4 4 5 6 1 8 3 2 7 0 8 7 0 3 6 2 5 4 1 8 6 4 2 1 7 5 3 0 0 7 6 8 2 1 4 5 3 6 1 3 7 4 2 8 5 0
CSC 225 - SPRING 2017 ALGORITHMS AND DATA STRUCTURES I PROGRAMMING ASSIGNMENT 4 UNIVERSITY OF VICTORIA Due: Friday, March 31st at 11:30pm. 1 Programming Assignment The 9-puzzle consists of a square grid containing eight tiles, marked with the numbers 1 through 8. One of the spaces in the grid is empty. The initial state of the puzzle is the conﬁguration below: This is considered to be the ‘solved’ state of the puzzle and is normally called the ‘goal state’. The tiles on the board can be moved horizontally or vertically into the empty space to scramble the ordering of the board, as in the conﬁguration below: The programming assignment is to implement a solver for the 9-puzzle. Your sub- mission will read a sequence of boards and, for each board, output a sequence of moves which solves the board. The information about the puzzle given here is meant 1
to be a basic summary; for more detailed information, see the additional notes in the resources tab of conneX 1 . The diﬀerent conﬁgurations of the 9-puzzle and their relationships can be mod- elled with a graph. Each vertex corresponds to a possible conﬁguration of the board. Edges represent the transformations possible by making one (legal) move. That is, if two diﬀerent board conﬁgurations are connected by an edge, there is a way to obtain one conﬁguration from the other by making a single move. To solve a given board, tiles are moved until the goal state is reached. The diagram below shows a small snapshot of the graph, with the goal state framed in green. The set of moves needed to solve a given board is represented by a path in the graph from the board to the goal state. Therefore, a board can be solved by performing one of the two fundamental graph traversals - DFS or BFS - on the graph and searching for a path to the goal state. Some possible board conﬁgurations cannot be solved, such as the following: 1 The Wikipedia page http://en.wikipedia.org/wiki/15_puzzle also contains some helpful information about puzzles of this type. 2
Show entire document
/* NinePuzzle.java CSC 225 - Spring 2017 Assignment 4 - Template for the 9-puzzle This template includes some testing code to help verify the implementation. Input boards can be provided with standard input or read from a file. To provide test inputs with standard input, run the program with java NinePuzzle To terminate the input, use Ctrl-D (which signals EOF). To read test inputs from a file (e.g. boards.txt), run the program with java NinePuzzle boards.txt The input format for both input methods is the same. Input consists of a series of 9-puzzle boards, with the '0' character representing the empty square. For example, a sample board with the middle square empty is 1 2 3 4 0 5 6 7 8 And a solved board is 1 2 3 4 5 6 7 8 0 An input file can contain an unlimited number of boards; each will be processed separately. B. Bird - 07/11/2014 M. Simpson - 11/07/2015 */ import java.util.Scanner; import java.io.File; public class NinePuzzle{ //The total number of possible boards is 9! = 1*2*3*4*5*6*7*8*9 = 362880 public static final int NUM_BOARDS = 362880; /* SolveNinePuzzle(B) Given a valid 9-puzzle board (with the empty space represented by the value 0),return true if the board is solvable and false otherwise. If the board is solvable, a sequence of moves which solves the board will be printed, using the printBoard function below. */ public static boolean SolveNinePuzzle(int[][] B){ /* . .. Your code here . .. */ return false; } /* printBoard(B) Print the given 9-puzzle board. The SolveNinePuzzle method above should use this method when printing the sequence of moves which solves the input
board. If any other method is used (e.g. printing the board manually), the submission may lose marks. */ public static void printBoard(int[][] B){ for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++) System.out.printf("%d ",B[i][j]); System.out.println(); } System.out.println(); } /* Board/Index conversion functions These should be treated as black boxes (i.e. don't modify them, don't worry about understanding them). The conversion scheme used here is adapted from W. Myrvold and F. Ruskey, Ranking and Unranking Permutations in Linear Time, Information Processing Letters, 79 (2001) 281-284. */ public static int getIndexFromBoard(int[][] B){ int i,j,tmp,s,n; int[] P = new int[9]; int[] PI = new int[9]; for (i = 0; i < 9; i++){ P[i] = B[i/3][i%3]; PI[P[i]] = i; } int id = 0; int multiplier = 1; for(n = 9; n > 1; n--){ s = P[n-1]; P[n-1] = P[PI[n-1]]; P[PI[n-1]] = s; tmp = PI[s]; PI[s] = PI[n-1]; PI[n-1] = tmp; id += multiplier*s; multiplier *= n; } return id; } public static int[][] getBoardFromIndex(int id){ int[] P = new int[9]; int i,n,tmp; for (i = 0; i < 9; i++) P[i] = i; for (n = 9; n > 0; n--){ tmp = P[n-1]; P[n-1] = P[id%n]; P[id%n] = tmp; id /= n; } int[][] B = new int[3][3]; for(i = 0; i < 9; i++) B[i/3][i%3] = P[i]; return B; }
Show entire document

10433461.zip - Preview not available

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
• -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents