Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have these two classes Board Class.......................................................................... import java.util.Arrays; import java.lang.System; public class Board { //public Board(int[][] blocks) construct a board from an N-by-N array

I have these two classes

Board Class..........................................................................

import java.util.Arrays;

import java.lang.System;

public class Board { //public Board(int[][] blocks) construct a board from an N-by-N array of blocks // (where blocks[i][j] = block in row i, column j) //public int dimension() board dimension N //public int hamming() number of blocks out of place //public int manhattan() sum of Manhattan distances between blocks and goal //public boolean isGoal() is this board the goal board? //public Board twin() a board obtained by exchanging two adjacent blocks in the same row //public boolean equals(Object y) does this board equal y? //public Iterable neighbors() all neighboring boards //public String toString() string representation of the board (in the output format specified below) private final int[][] tiles; private int size; private int zeroCol; private int zeroRow; private int hamming; private int manhattan;

public Board(int[][] blocks){ size = blocks.length; tiles =new int[size][size]; for(int i = 0; i

public int hamming(){ int arrayPosition; int tile; int displaced =0; for(int i = 0; i

for(int i = 0; i neighbors(){ Stack boards = new Stack();

if(zeroRow > 0){ Board boardUP = new Board(swap(tiles,-1,0)); boards.push(boardUP); } if(zeroRow < size-1){ Board boardDown = new Board(swap(tiles,1,0)); boards.push(boardDown); } if(zeroCol > 0){ Board boardLeft = new Board(swap(tiles,0,-1)); boards.push(boardLeft); } if(zeroCol

return boards; } public int[][] swap(int[][] board, int rowOffset, int colOffset){ int[][] tempBoard = deepCopy(board); tempBoard[zeroRow][zeroCol]= tiles[zeroRow+rowOffset][zeroCol+colOffset]; tempBoard[zeroRow+rowOffset][zeroCol+colOffset]=0;

return tempBoard; } }

Solver Class....................................................................

public class Solver {

//public Solver(Board initial) // find a solution to the initial board (using the A* algorithm) //public boolean isSolvable() // is the initial board solvable? //public int moves() // min number of moves to solve initial board; -1 if no solution //public Iterable solution() // sequence of boards in a shortest solution; null if no solution //public static void main(String[] args) // solve a slider puzzle (given below) // you will want to use comparator for hamming & man //MinPq pq as the priority queue

private Node goalNode; private MinPQ pq = new MinPQ(); private MinPQ pqTwin = new MinPQ(); public class Node implements Comparable{ public Board board; public Node previous; public int moves;

public int compareTo(Node that){ //StdOut.println("i:" + this.priority() + " j:" + that.priority() + " "+ ((this.priority() > that.priority()) ? 1 : -1)); if(this.priority() == that.priority()) return 0; return (this.priority() > that.priority()) ? 1 : -1; } public Node(Board b, Node prev, int m){ board = b; previous = prev; moves = m; } public int priority(){ return board.manhattan() + moves; } } public Solver(Board initial){ Board initialBoard; Queue neighbors = new Queue(); initialBoard = initial;

Node currentNode = new Node(initial, null, 0); Node currentTwin = new Node(initial.twin(), null, 0); pq.insert(currentNode); pqTwin.insert(currentTwin); while(!currentNode.board.isGoal() && !currentNode.board.isGoal()){

currentNode = pq.delMin(); currentTwin = pqTwin.delMin();

for(Board b : currentNode.board.neighbors()) { if(!b.equals(currentNode.board)) pq.insert(new Node(b, currentNode, currentNode.moves +1)); } for(Board b : currentTwin.board.neighbors()) { if(!b.equals(currentNode.board)) pqTwin.insert(new Node(b, currentTwin, currentTwin.moves +1)); } } if(currentNode.board.isGoal()) goalNode = currentNode; else goalNode = currentTwin; } public Iterable solution(){ Queue trace = new Queue(); trace.enqueue(goalNode.board); while (goalNode.previous != null){ goalNode = goalNode.previous; trace.enqueue(goalNode.board); } return trace; } public boolean isSolvable(){ return goalNode != null; } public int moves(){ return goalNode.moves; } public static void main(String[] args) { // create initial board from file In in = new In(args[0]); int N = in.readInt(); int[][] blocks = new int[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) blocks[i][j] = in.readInt(); Board initial = new Board(blocks); // solve the puzzle Solver solver = new Solver(initial); // print solution to standard output if (!solver.isSolvable()) StdOut.println("No solution possible"); else { StdOut.println("Minimum number of moves = " + solver.moves()); for (Board board : solver.solution()) StdOut.println(board); } } }

My problem is that in the solver class, I'm getting a couple of errors. In the line

In in = new In(args[0]);

In cannot be resolved to a type, and in the line

StdOut.println("No solution possible");

StdOut cannot be resolved. Does anyone have a solution to fix these problems. Accompanying code, would be helpful

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

2. Why has the conflict escalated?

Answered: 1 week ago

Question

1. What might have led to the misinformation?

Answered: 1 week ago