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
}
------------------
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started