30 Mar Complete the puzzle method by using heuristic search f(n) = g(n)+ h(n).For h(n) use sum of distances out of place (hamilton distance).Make sure to write your name in main method.3- Create an
import java.io.*; import java.util.*; // 8 puzzle program public class xxxxx8puz { //class variables //use prt for System.out to save typing PrintStream prt = System.out; //Print Graph Matrix private void prtStates(int cur[], int nxt[], String mov){ int j, k, m, n; // Print Graph matrix prt.printf("ntFrom cur state to nxt state using %s move:nt", mov); for (j = 0; j < 3; j++){ m = j * 3; n = m + 3; for (k = m; k < n; k++) prt.printf("%3d", cur[k]); //end for k =m prt.printf("t"); for (k = m; k < n; k++) prt.printf("%3d", nxt[k]); //end for k =m prt.printf("nt"); } //end for j =1 } // end prtStates // 8-puuzle from current sate to goal state private void puzzle(int cs[], int gs[]){ //complete this method } // end puzzle private void process(String fn) { // read input int cs[], gs[]; //initial and goal state int i, j, n; long start, end; prt.printf("nt8 Puzzle program:"+ "ntThis method reads initial state and goal state from inputfile"+ "ntand prints all the moves from initial state to goal state."+ "nttTo compile: javac xxxxx8puz.java" + "nttTo execute: java xxxxx8puz inputfilename"+ "ntt Example: java xxxxx8puz inputfilename"); //Allocate space for initial and goal states cs = new int[9]; gs = new int[9]; try{ // open input file Scanner inf = new Scanner(new File(fn)); n = inf.nextInt();//read no. of cases // read initial and goal states for (i = 1; i <= n; i++){ // read current state for (j = 0; j <= 8; j++) cs[j]= inf.nextInt(); // end for j // read goal state for (j = 0; j <= 8; j++) gs[j]= inf.nextInt(); // end for j //initial state and goal state prtStates(cs, gs, "??"); //Get System Time start = System.currentTimeMillis(); puzzle(cs, gs); //Get System Time end = System.currentTimeMillis(); prt.printf("ntExec Time: %d ms", end – start); } // end for i // close input file inf.close(); }catch(IOException e) {prt.printf("nI/O Error %s", e );} } // end process method public static void main(String[] args) throws Exception{ int cnt = args.length; // get no. of arguments String fn; if (cnt < 1){ System.out.printf("ntOOOPS Invalid No. of aguments!"+ "ntTO Execute: java xxxxx8puz inputfilename"); return; } // end if // get input file name fn = args[0]; // create an instance of xxxxx8puz class xxxxx8puz g = new xxxxx8puz (); // call process method g.process(fn); //Replace ????????? with your name in next line System.out.printf("nntAuthor: ????????? Date: %sn", java.time.LocalDate.now()); } // end main } // end xxxxx8puz /*Sample 8-puzzle input 3 1 3 2 5 0 4 8 7 6 1 3 2 5 7 4 8 6 0 1 5 2 0 8 6 3 7 4 1 2 3 4 5 0 8 7 6 1 3 2 5 0 4 8 7 6 0 1 3 2 5 7 4 6 8 */
