Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5. public class ccc10j5 {
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. int startR = sc.nextInt()-1;
  9. int startC = sc.nextInt()-1;
  10. int endR = sc.nextInt()-1;
  11. int endC = sc.nextInt()-1;
  12.  
  13. int[][] step = new int[8][8];
  14. //fill with Integer.MAX_VALUE
  15. for (int r=0; r<8; r++) {
  16. Arrays.fill(step[r], Integer.MAX_VALUE);
  17. }
  18.  
  19. LinkedList<Integer> rQ = new LinkedList<Integer>();
  20. LinkedList<Integer> cQ = new LinkedList<Integer>();
  21.  
  22. //initialize beginning location
  23. step[startR][startC] = 0;
  24. rQ.add(startR);
  25. cQ.add(startC);
  26.  
  27. while(!rQ.isEmpty()) {
  28. int r = rQ.poll(); //read current position's row
  29. int c = cQ.poll();
  30.  
  31. //1. r-2 c+1
  32. if (r-2>=0 && c+1<8 //check the boudary
  33. && step[r-2][c+1]>step[r][c]+1) {
  34. step[r-2][c+1] = step[r][c]+1;
  35. rQ.add(r-2);
  36. cQ.add(c+1);
  37. }
  38. //2. r-1 c+2
  39. if (r-1>=0 && c+2<8 //check the boudary
  40. && step[r-1][c+2]>step[r][c]+1) {
  41. step[r-1][c+2] = step[r][c]+1;
  42. rQ.add(r-1);
  43. cQ.add(c+2);
  44. }
  45. //3. r+1 c+2
  46. //4. r+2 c+1
  47. //5. r+2 c-1
  48. //6. r+1 c-2
  49. //7 r-1 c-2
  50. //8 r-2 c-1
  51. }
  52.  
  53. System.out.println(step[endR][endC]);
  54.  
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement