Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pt.ipleiria.estg.dei.aed.recursividade.algoritmos;
- import pt.ipleiria.estg.dei.aed.utils.EstatisticaDeChamadasEMovimentos;
- public class Hanoi {
- public void executar(int n, Torre inicial, Torre fim, Torre aux, boolean mostrar, EstatisticaDeChamadasEMovimentos estatistica) {
- estatistica.incrementarChamadas();
- if (n <= 0) {
- if (n <= 0) {
- throw new IllegalArgumentException("hanoi(" + n + ") = argumento inválido: deve ser > 0");
- }
- }
- if (n == 1) {
- estatistica.incrementarMovimentos();
- fim.adicionarDisco(inicial.removerDisco());
- if (mostrar) {
- System.out.println("Disco " + n + " movido da torre " + inicial + " para a torre " + fim + ".");
- }
- return;
- }
- executar(n - 1, inicial, aux, fim, mostrar, estatistica);
- estatistica.incrementarMovimentos();
- if (mostrar) {
- System.out.println("Disco " + n + " movido da torre " + inicial + " para a torre " + fim + ".");
- }
- executar(n - 1, aux, fim, inicial, mostrar, estatistica);
- }
- public EstatisticaDeChamadasEMovimentos getEstatistica(int n, boolean mostrar) {
- try {
- Torre inicial = new Torre("incial", n);
- Torre aux = new Torre("aux",n);
- Torre fim = new Torre("final",n);
- inicial.preencherComDiscos();
- EstatisticaDeChamadasEMovimentos estatistica = new EstatisticaDeChamadasEMovimentos(n);
- executar(n, inicial, aux, fim, mostrar, estatistica);
- estatistica.parar();
- estatistica.apresentar();
- return estatistica;
- } catch (RuntimeException exception) {
- System.err.println(exception.getMessage() + "\n");
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement