import edu.princeton.cs.algs4.In;import java.util.ArrayList;import java.util.Arrays;publicclassBoard{
privatefinalint[][] board;privatefinalint n;// create a board from an n-by-n array of tiles,// where tiles[row][col] = tile at (row, col)publicBoard(int[][] tiles){
board =newint[tiles.length][tiles.length];
n = tiles.length;for(int i =0; i < n; i++)for(int j =0; j < n; j++)
board[i][j]= tiles[i][j];}// string representation of this boardpublic String toString(){
StringBuilder s =newStringBuilder();
s.append(board.length +"\n");for(int i =0; i < board.length; i++){
for(int j =0; j < board.length; j++)
s.append(String.format("%2d ", board[i][j]));
s.append("\n");}return s.toString();}// board dimension npublicintdimension(){
return n;}privateintgetIndex(int row,int col){
return row * n + col +1;}// number of tiles out of placepublicinthamming(){
int ham =0;for(int i =0; i < n; i++){
for(int j =0; j < n; j++){
if(getIndex(i, j)== n * n)break;if(board[i][j]!=getIndex(i, j))
ham++;}}return ham;}// sum of Manhattan distances between tiles and goalpublicintmanhattan(){
int manh =0;for(int i =0; i < n; i++){
for(int j =0; j < n; j++){
if(board[i][j]==0){
continue;}int row =(board[i