Я понимаю, что решение далеко от идеала, просто специально ничего не гуглил и не читал комментарии. Работает идеально до 14 колец включительно.
package com.javarush.task.task34.task3411;
/*
Ханойские башни
*/
import java.util.*;
public class Solution {
static Deque<Integer> A = new ArrayDeque<>();
static Deque<Integer> B = new ArrayDeque<>();
static Deque<Integer> C = new ArrayDeque<>();
static int totalMoves = 0;
static int[][] moves = new int[3][2];
static boolean isEven;
static int variantN;
static int variantM;
static boolean isMoved;
public static void main(String[] args) {
moveRing('A', 'B', 'C', 3);
}
public static void moveRing(char a, char b, char c, int numRings) {
if (numRings > 0){
for (int i = numRings; i > 0; i--) {
A.add(i);
}
moves[2][0] = 1;
if (numRings % 2 == 0){
isEven = true;
moves[2][1] = 3;
} else {
isEven = false;
moves[2][1] = 2;
}
}
moves[0][0] = moves[2][0];
moves[0][1] = moves[2][1];
if (isEven) {
moves[2][0] = increaseValue(increaseValue(moves[2][0]));
moves[2][1] = increaseValue(increaseValue(moves[2][1]));
} else {
moves[2][0] = increaseValue(moves[2][0]);
moves[2][1] = increaseValue(moves[2][1]);
}
switch (moves[0][0]){
case 1:{
if (moves[2][0] == 2) variantN = 3; else variantN = 2;
break;
}
case 2:{
if (moves[2][0] == 1) variantN = 3; else variantN = 1;
break;
}
case 3:{
if (moves[2][0] == 1) variantN = 2; else variantN = 1;
break;
}
}
switch (moves[0][1]){
case 1:{
if (moves[2][1] == 2) variantM = 3; else variantM = 2;
break;
}
case 2:{
if (moves[2][1] == 1) variantM = 3; else variantM = 1;
break;
}
case 3:{
if (moves[2][1] == 1) variantM = 2; else variantM = 1;
break;
}
}
isMoved = move(moves[0], a, b, c);
if (check()) return;
if (!move(new int[]{variantN, variantM}, a, b, c)){
move(new int[]{variantM, variantN}, a, b, c);
}
if (check()) return;
moveRing(a, b, c, 0);
}
public static boolean check(){
return A.peek() == null && C.peek() == null;
}
public static int increaseValue(int n){
n++;
if (n == 4){
n = 1;
}
return n;
}
public static boolean move(int[] moves, char a, char b, char c){
Deque<Integer> n1 = new ArrayDeque<>();
Deque<Integer> n2 = new ArrayDeque<>();
char c1 = ' ';
char c2 = ' ';
switch (moves[0]){
case 1:{
n1 = A;
c1 = a;
break;
}
case 2:{
n1 = B;
c1 = b;
break;
}
case 3:{
n1 = C;
c1 = c;
break;
}
}
switch (moves[1]){
case 1:{
n2 = A;
c2 = a;
break;
}
case 2:{
n2 = B;
c2 = b;
break;
}
case 3:{
n2 = C;
c2 = c;
break;
}
}
if (n2.peekLast() != null && n1.peekLast() != null) {
if (n2.peekLast() > n1.peekLast()) {
n2.add(n1.pollLast());
System.out.println("from " + c1 + " to " + c2);
//draw();
return true;
}
}
if (n2.peekLast() == null && n1.peekLast() != null ) {
n2.add(n1.pollLast());
System.out.println("from " + c1 + " to " + c2);
//draw();
return true;
}
return false;
}
public static void draw(){
/*
List<Integer> qAcopy= new ArrayList<>(A);
List<Integer> qBcopy= new ArrayList<>(B);
List<Integer> qCcopy= new ArrayList<>(C);
System.out.print(">");
for (int i: qAcopy) {
System.out.print(i);
}
System.out.println();
System.out.print(">");
for (int i: qBcopy) {
System.out.print(i);
}
System.out.println();
System.out.print(">");
for (int i: qCcopy) {
System.out.print(i);
}
System.out.println();
System.out.println("--- " + totalMoves++ + " move ---");
*/
}
}
