Towers, Hana Towers.



  • This is a challenge to the Hanoi pyramids through the competition. It needs to be completed and valued exchange(?,?,?,?) Here's the code.

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            System.out.println("i'm in onCreate");
            int[] arg = {1,2,3};
            main(arg);
        }
        public static void main(int[] arg){
            Stack<Integer> from = new Stack<>();
            Stack<Integer> help = new Stack<>();
            Stack<Integer> to = new Stack<>();
            from.push(3);
            from.push(2);
            from.push(1);
            System.out.println("print from = " + from);
            exchange(from, help, to, from.size());
            System.out.println("print to = "+ to);
    
    }
    
    private static void exchange(Stack&lt;Integer&gt; from, Stack&lt;Integer&gt; help, Stack&lt;Integer&gt; to, int count) {
        if (count &gt; 0){
            exchange(?,?,?,?);
            int biggest = from.pop();
            to.push(biggest);
            exchange(?,?,?,?);
        }
    }
    

    }

    What I realized and what I did:

    1. Because I've been compiling android studio without method onCreate Nowhere, it's launched first, mass arg Established to launch subsequent methods.

    2. I understand. Stack organized on a principle FILO

    3. Methods push and pop Storage and removal, respectively.

    4. In particular, how the method works exchange

    5.B exchange(?,?,?,?) set the values in the first case exchange(from,help,to,3)Second exchange(to,help,from,1) after which the program is cycling.



  • The challenge is to &quot; move the tower from n-1 to 2nd. &quot; Then we carry the largest disc on the 3rd bay, and we are responding to the challenge of moving the tower from n-1 to 3rd bay.

    private static void exchange(Stack<Integer> from, Stack<Integer> help, Stack<Integer> to, int count) {
        if (count > 0){
            exchange(from,to,help,count-1); // перенести башню из n−1 диска на 2-й штырь
            int biggest = from.pop();
            to.push(biggest); // переносим самый большой диск на 3-й штырь
            exchange(help,from,to,count-1); // перенеси башню из n−1 диска на 3-й штырь
        }
    }
    

    https://ru.wikipedia.org/wiki/%D0%A5%D0%B0%D0%BD%D0%BE%D0%B9%D1%81%D0%BA%D0%B0%D1%8F_%D0%B1%D0%B0%D1%88%D0%BD%D1%8F#.D0.A0.D0.B5.D0.BA.D1.83.D1.80.D1.81.D0.B8.D0.B2.D0.BD.D0.BE.D0.B5_.D1.80.D0.B5.D1.88.D0.B5.D0.BD.D0.B8.D0.B5 assistance:




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2