Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package petch16.assignment;
  7.  
  8. /**
  9. *
  10. * @author John
  11. */
  12. import java.util.*;
  13.  
  14. // Java program to find minimum steps to reach to
  15. // specific cell in minimum moves by Knight
  16. class Main
  17. {
  18.  
  19. // Class for storing a cell's data
  20. static class cell
  21. {
  22. int x, y;
  23. int dis;
  24.  
  25. public cell(int x, int y, int dis)
  26. {
  27. this.x = x;
  28. this.y = y;
  29. this.dis = dis;
  30. }
  31.  
  32.  
  33. }
  34.  
  35. // Utility method returns true if (x, y) lies
  36. // inside Board
  37. static boolean isInside(int x, int y, int N)
  38. {
  39. if (x >= 1 && x <= N && y >= 1 && y <= N)
  40. return true;
  41. return false;
  42. }
  43.  
  44. // Method returns minimum step
  45. // to reach target position
  46. static int minStepToReachTarget(int knightPos[], int targetPos[],
  47. int N)
  48. {
  49. // x and y direction, where a knight can move
  50. int dx[] = {-2, -1, 1, 2, -2, -1, 1, 2};
  51. int dy[] = {-1, -2, -2, -1, 1, 2, 2, 1};
  52.  
  53. // queue for storing states of knight in board
  54. Vector<cell> q = new Vector<>();
  55.  
  56. // push starting position of knight with 0 distance
  57. q.add(new cell(knightPos[0], knightPos[1], 0));
  58.  
  59. cell t ;
  60. int x, y;
  61. boolean visit[][] = new boolean[N + 1][N + 1];
  62.  
  63. // make all cell unvisited
  64. for (int i = 1; i <= N; i++)
  65. for (int j = 1; j <= N; j++)
  66. visit[i][j] = false;
  67.  
  68. // visit starting state
  69. visit[knightPos[0]][knightPos[1]] = true;
  70.  
  71. // loop untill we have one element in queue
  72. while (!q.isEmpty())
  73. {
  74. t = q.firstElement();
  75. q.remove(0);
  76.  
  77. // if current cell is equal to target cell,
  78. // return its distance
  79. if (t.x == targetPos[0] && t.y == targetPos[1])
  80. return t.dis;
  81.  
  82. // loop for all reachable states
  83. for (int i = 0; i < 8; i++)
  84. {
  85. x = t.x + dx[i];
  86. y = t.y + dy[i];
  87.  
  88. // If reachable state is not yet visited and
  89. // inside board, push that state into queue
  90. if (isInside(x, y, N) && !visit[x][y])
  91. {
  92. visit[x][y] = true;
  93. q.add(new cell(x, y, t.dis + 1));
  94. }
  95. }
  96. }
  97. return Integer.MAX_VALUE;
  98. }
  99.  
  100. // Driver code
  101. public static void main(String[] args)
  102. {
  103. int N = 500;
  104. int knightPos[] = {1, 1};
  105. int targetPos[] = {299, 299};
  106. System.out.println(minStepToReachTarget(knightPos, targetPos, N));
  107. }
  108. }
  109.  
  110. // This code contributed by Rajput-Ji
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement