Esempio n. 1
0
        /*TODO: Implementar métodos*/
        public void mover_disco(Pila a, Pila b)
        {
            if (a.peek() != 99)//si es 99 la pila esta vacia
            {
                Disco movido = a.pop();
                b.push(movido);
            }

            else
            {
                if (b.peek() != 99)//si es 99 la pila esta vacia
                {
                    Disco movido = b.pop();
                    a.push(movido);
                }
            }
        }//mover_disco
Esempio n. 2
0
        static void Main(string[] args)
        {
            Disco peque  = new Disco(1);
            Disco medio  = new Disco(2);
            Disco grande = new Disco(3);

            Pila ini = new Pila();

            ini.push(grande);
            ini.push(medio);
            ini.push(peque);
            Pila aux = new Pila();
            Pila fin = new Pila();

            Hanoi nuevoHanoi = new Hanoi();
            //ini-->fin
            //ini-->aux
            //fin-->aux
            //ini-->fin
            //aux-->ini
            //aux-->fin
            //ini-->fin

            var res = nuevoHanoi.iterativo(7, ini, aux, fin);

            Console.WriteLine(res);


            var resultado0 = ini.peek();

            Console.WriteLine(resultado0);
            var resultado1 = aux.peek();

            Console.WriteLine(resultado1);
            var resultado2 = fin.peek();

            Console.WriteLine(resultado2);

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Esempio n. 3
0
        }//mover_disco

        //ini-->fin
        //ini-->aux
        //fin-->aux
        //ini-->fin
        //aux-->ini
        //aux-->fin
        //ini-->fin
        public int iterativo(int n, Pila inicial, Pila final, Pila auxiliar)
        {
            var resultado = 0;

            // var contador = 0;

            if (n % 2 != 0)//impar
            {
                for (var i = 0; i < n; i++)
                {
                    mover_disco(inicial, final);
                    mover_disco(inicial, auxiliar);
                    mover_disco(final, auxiliar);
                    //contador++;
                } //for
            }     //if

            else  //par
            {
                for (var i = 0; i < n; i++)
                {
                    mover_disco(inicial, auxiliar);
                    mover_disco(inicial, final);
                    mover_disco(auxiliar, final);
                } //for
            }     //else

            if (final.size() == 3 && final.peek() == 1)
            {
                resultado = 1;
            }

            return(resultado);

            // return contador;
        }