Advertisement
ramprakash110109

8NumberPuzzle

Jun 4th, 2022
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. package puzzle;
  2. import java.util.*;
  3.  
  4. import puzzle.Puzzle.Node;
  5. import puzzle.Puzzle.comp;
  6.  
  7. public class Puzzle1 {
  8. public static int N =3;
  9. public static class Node{
  10. Node parent;
  11. int mat[][] = new int[N][N];
  12. int x,y;
  13. int cost;
  14. int level;
  15. }
  16. public static void printPath(Node root) {
  17. if(root == null)
  18. return;
  19. printPath(root.parent);
  20. printMatrix(root.mat);
  21. System.out.println("");
  22. }
  23. public static void printMatrix(int[][] mat) {
  24. for(int i=0;i<N;i++) {
  25. for(int j=0;j<N;j++) {
  26. System.out.print(mat[i][j] + " ");
  27. }
  28. System.out.println("");
  29. }
  30. }
  31. public static Node newNode(int[][] initialState,int x,int y,int newX,int newY,int level,Node parent) {
  32. Node node = new Node();
  33. node.parent = parent;
  34. node.mat = new int[N][N];
  35. for(int i=0;i<N;i++)
  36. for(int j=0;j<N;j++)
  37. node.mat[i][j] = initialState[i][j];
  38. int temp = node.mat[x][y];
  39. node.mat[x][y] = node.mat[newX][newY];
  40. node.mat[newX][newY] = temp;
  41.  
  42. node.cost = Integer.MAX_VALUE;
  43. node.level = level;
  44. node.x = newX;
  45. node.y = newY;
  46. return node;
  47. }
  48. public static int[] row = {1,0,-1,0};
  49. public static int[] col = {0,-1,0,1};
  50. public static int calculateCost(int[][] initialState, int[][] finalState) {
  51. int count = 0;
  52. for(int i=0;i<N;i++)
  53. for(int j=0;j<N;j++)
  54. if(initialState[i][j]!=0 && initialState[i][j]!=finalState[i][j])
  55. count++;
  56. return count;
  57. }
  58. public static int isSafe(int x, int y) {
  59. return (x >=0 && x < N && y >=0 && y < N) ? 1:0;
  60. }
  61. public static class comp implements Comparator<Node>{
  62. @Override
  63. public int compare(Node lhs, Node rhs) {
  64. return (lhs.cost+lhs.level)>(rhs.cost + rhs.level)?1:-1;
  65. }
  66. }
  67. public static void solve(int[][] initialState, int x, int y, int[][] finalState) {
  68. PriorityQueue<Node> pq = new PriorityQueue<>(new comp());
  69. Node root = newNode(initialState,x,y,x,y,0,null);
  70. root.cost = calculateCost(initialState,finalState);
  71. pq.add(root);
  72. while(!pq.isEmpty()) {
  73. Node min = pq.peek();
  74. pq.poll();
  75. if(min.cost ==0) {
  76. printPath(min);
  77. return;
  78. }
  79. for(int i=0;i<4;i++) {
  80. if(isSafe(min.x + row[i], min.y + col[i]) > 0) {
  81. Node child = newNode(min.mat,min.x,min.y,min.x+row[i],min.y+col[i],min.level + 1,min);
  82. child.cost = calculateCost(child.mat,finalState);
  83. pq.add(child);
  84. }
  85. }
  86. }
  87. }
  88. public static void main(String[] args) {
  89. try{
  90. int initialState[][] = {{1,2,3},{5,6,0},{7,8,4}};
  91. int finalState[][] = {{1,2,3},{5,8,6},{0,7,4}};
  92. int x = 1, y = 2; // Blank tile coordinates in initialState
  93. solve(initialState, x, y, finalState);
  94. }catch(Exception e) {
  95. System.out.println("Exception is " + e);
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement