Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. package lab14;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Main {
  8.  
  9. int tab[][] = new int[8][8];
  10. boolean flaga = false;
  11.  
  12. public static void main(String[] args) {
  13. Main kl = new Main();
  14. kl.skoczek(4,4,1);
  15. kl.wieza();
  16.  
  17. }
  18.  
  19.  
  20. public void skoczek(int x, int y, int i) {
  21. if (i == (8 * 8 + 1)) {
  22. for (int[] b : tab) {
  23. System.out.println(Arrays.toString(b));
  24. }
  25. flaga = true;
  26. System.out.println();
  27. return;
  28. }
  29. if (x < 0 || y < 0) {
  30. return;
  31. }
  32. if (x >= 8 || y >= 8) {
  33. return;
  34. }
  35. if (tab[x][y] != 0) {
  36. return;
  37. }
  38. tab[x][y] = i;
  39.  
  40.  
  41. if (!flaga) skoczek(x - 2, y + 1, i + 1);
  42. if (!flaga) skoczek(x - 2, y - 1, i + 1);
  43. if (!flaga) skoczek(x + 2, y + 1, i + 1);
  44. if (!flaga) skoczek(x + 2, y - 1, i + 1);
  45.  
  46. if (!flaga) skoczek(x - 1, y + 2, i + 1);
  47. if (!flaga) skoczek(x - 1, y - 2, i + 1);
  48. if (!flaga) skoczek(x + 1, y + 2, i + 1);
  49. if (!flaga) skoczek(x + 1, y - 2, i + 1);
  50.  
  51. tab[x][y] = 0;
  52. }
  53.  
  54. public void wieza() {
  55. int i;
  56.  
  57. System.out.println("1 - palik zrodlowy");
  58. System.out.println("2 - palik pomocniczy");
  59. System.out.println("3 - palik docelowy");
  60. i = new Scanner(System.in).nextInt();
  61.  
  62. hanoi(1, 3, i);
  63. }
  64.  
  65. public void hanoi(int skad, int cel, int n) {
  66. int helper = 6 - (skad + cel);
  67.  
  68. if (n == 1)
  69. System.out.println(skad + " -> " + cel);
  70. else {
  71. hanoi(skad, helper, n - 1);
  72. hanoi(skad, cel, 1);
  73. hanoi(helper, cel, n - 1);
  74. }
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement