Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab14;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- int tab[][] = new int[8][8];
- boolean flaga = false;
- public static void main(String[] args) {
- Main kl = new Main();
- kl.skoczek(4,4,1);
- kl.wieza();
- }
- public void skoczek(int x, int y, int i) {
- if (i == (8 * 8 + 1)) {
- for (int[] b : tab) {
- System.out.println(Arrays.toString(b));
- }
- flaga = true;
- System.out.println();
- return;
- }
- if (x < 0 || y < 0) {
- return;
- }
- if (x >= 8 || y >= 8) {
- return;
- }
- if (tab[x][y] != 0) {
- return;
- }
- tab[x][y] = i;
- if (!flaga) skoczek(x - 2, y + 1, i + 1);
- if (!flaga) skoczek(x - 2, y - 1, i + 1);
- if (!flaga) skoczek(x + 2, y + 1, i + 1);
- if (!flaga) skoczek(x + 2, y - 1, i + 1);
- if (!flaga) skoczek(x - 1, y + 2, i + 1);
- if (!flaga) skoczek(x - 1, y - 2, i + 1);
- if (!flaga) skoczek(x + 1, y + 2, i + 1);
- if (!flaga) skoczek(x + 1, y - 2, i + 1);
- tab[x][y] = 0;
- }
- public void wieza() {
- int i;
- System.out.println("1 - palik zrodlowy");
- System.out.println("2 - palik pomocniczy");
- System.out.println("3 - palik docelowy");
- i = new Scanner(System.in).nextInt();
- hanoi(1, 3, i);
- }
- public void hanoi(int skad, int cel, int n) {
- int helper = 6 - (skad + cel);
- if (n == 1)
- System.out.println(skad + " -> " + cel);
- else {
- hanoi(skad, helper, n - 1);
- hanoi(skad, cel, 1);
- hanoi(helper, cel, n - 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement