Question: Consider the 8-puzzle: 8 tiles each numbered 1-8 on a 3x3 grid, with one space empty. Any adjacent tile can be moved into the empty

Consider the 8-puzzle: 8 tiles each numbered 1-8 on a 3x3 grid, with one space empty. Any adjacent tile can be moved into the empty square (in effect swapping the locations of that tile and the empty square). The goal is, to begin with, some arbitrary arrangement and end with the tiles in the following arrangement: 123 456 78E where E denotes the empty square. One possible complication is that permutations of the game board fall into 2 disjoint sets of odd or even parity, only one of which can reach the goal. Thus, half of all possible tile arrangements cannot lead to a solution. Your program must correctly detect whether a solution is possible or not. Your input file is a simple text file. The first element, on a line by itself, is the number of puzzles contained in the file. After that will be the specified number of puzzles in the above format (3 lines of 3 characters each, each character will be a digit 1-8 or an upper case E). Each puzzle is separated from the next by a blank line. Your program will be tested on a different data file with the same format. ---------> I have the code, I want to change the outcome 0 to E.please help me add that part of the code to my java code that I will post here too.thanks

import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue;

public class NQueenProblem {

private static final int E = 0;

public int dimension = 3; // Bottom, left, top, right int[] row = { 1, 0, -1, 0 }; int[] col = { 0, -1, 0, 1 }; public int calculateCost(int[][] initial, int[][] goal) { int count = 0; int n = initial.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (initial[i][j] != 0 && initial[i][j] != goal[i][j]) { count++; } } } return count; } public void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } public boolean isSafe(int x, int y) { return (x >= 0 && x < dimension && y >= 0 && y < dimension); } public void printPath(Node root) { if (root == null) { return; } printPath(root.parent); printMatrix(root.matrix); System.out.println(); } public boolean isSolvable(int[][] matrix) { int count = 0; List array = new ArrayList(); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { array.add(matrix[i][j]); } } Integer[] anotherArray = new Integer[array.size()]; array.toArray(anotherArray); for (int i = 0; i < anotherArray.length - 1; i++) { for (int j = i + 1; j < anotherArray.length; j++) { if (anotherArray[i] != 0 && anotherArray[j] != 0 && anotherArray[i] > anotherArray[j]) { count++; } } } return count % 2 == 0; } public void solve(int[][] initial, int[][] goal, int x, int y) { PriorityQueue pq = new PriorityQueue(1000, (a, b) -> (a.cost + a.level) - (b.cost + b.level)); Node root = new Node(initial, x, y, x, y, 0, null); root.cost = calculateCost(initial, goal); pq.add(root); while (!pq.isEmpty()) { Node min = pq.poll(); if (min.cost == 0) { printPath(min); return; } for (int i = 0; i < 4; i++) { if (isSafe(min.x + row[i], min.y + col[i])) { Node child = new Node(min.matrix, min.x, min.y, min.x + row[i], min.y + col[i], min.level + 1, min); child.cost = calculateCost(child.matrix, goal); pq.add(child); } } } } public static void main(String[] args) { int[][] initial = { {1, 8, 2}, {0, 4, 3}, {7, 6, 5} }; int [][] goal1 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} }; // White tile coordinate int x = 1, y = 0; NQueenProblem puzzle = new NQueenProblem(); if (puzzle.isSolvable(initial)) { puzzle.solve(initial, goal1, x, y); } else { System.out.println("The given initial is impossible to solve"); } }

}

------------------

public class Node {

public Node parent; public int[][] matrix; // Blank tile coordinates public int x, y; // Number of misplaced tiles public int cost; // The number of moves so far public int level; public Node(int[][] matrix, int x, int y, int newX, int newY, int level, Node parent) { this.parent = parent; this.matrix = new int[matrix.length][]; for (int i = 0; i < matrix.length; i++) { this.matrix[i] = matrix[i].clone(); } // Swap value this.matrix[x][y] = this.matrix[x][y] + this.matrix[newX][newY]; this.matrix[newX][newY] = this.matrix[x][y] - this.matrix[newX][newY]; this.matrix[x][y] = this.matrix[x][y] - this.matrix[newX][newY]; this.cost = Integer.MAX_VALUE; this.level = level; this.x = newX; this.y = newY; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!