Hanoi Tower on C++. Iterative method



  • I'm studying C++ on the book, getting to the C++. After the chapter, there was a Hana Tower mission. The problem is: how to deal with this challenge in a different way, I understand myself, but in the book, it was suggested that the same challenge be met by the iterative method, and there were problems. In general, I have learned how to move the discs according to their number ' s numeracy or incorrectness: if the equivalent, the first disc is placed on the intermediate braces, otherwise on the finish line. In the decision, I was trying to move from one disc, gradually increasing the number of rescheduled disks, so if I had to move five disks from one to three, then I'd have to change the disk to three, and then I'm gonna have to move the next disk from the initial stack to two wheels, and then put two disks on it, and then put three-eyes on it. Trying to find a solution and stumbling on how to use the vectors and masses, it wasn't in my book yet, so I have to decide otherwise.



  • Here's to help you get a piece of the book. Ah. Shane. Programming: Theorems and tasks of the 2nd edition, M.: ICNMO, 2004, 296 s. http://www.mccme.ru/free-books/shen/shen-progbook.pdf ):

    8.2. 1. Write a non-curricular programme to find the sequence of movements of the ring in the task of the Hana Towers.

    Decision.

    We'll remember the recurring program. i Upper ring with mn:

    procedure move(i,m,n: integer);
      var s: integer;
    begin
      if i = 1 then begin
        writeln (’сделать ход ’, m, ’->’, n);
      end else begin
        s:=6-m-n; {s - третий стержень: сумма номеров равна 6}
        move (i-1, m, s);
        writeln (’сделать ход ’, m, ’->’, n);
        move (i-1, s, n);
      end;
    end;
    

    It's obvious that it's the task to move i top disk m- his bar on n- The three tasks of the same type are: two tasks. i-1 Discs and one single disk task. With these tasks, it is important not to forget what remains to be done.

    For this purpose, field glassthe elements of which the three will be (i,m,n)♪ Each of these trips is interpreted as an order to transfer i top disk m- his bar on n-." Orders are streamlined according to the required procedure of implementation: the most urgent is the top of the line. We get this program:

    procedure move(i,m,n: integer);
    begin
      сделать стек заказов пустым
      положить в стек тройку <i,m,n>
      {инвариант: осталось выполнить заказы в стеке}
      while стек непуст do begin
        удалить верхний элемент, переложив его в <j,p,q>
        if j = 1 then begin
          writeln (’сделать ход’, p, ’->’, q);
        end else begin
          s:=6-p-q;
            {s - третий стержень: сумма номеров равна 6}
          положить в стек тройки <j-1,s,q>, <1,p,q>, <j-1,p,s>
        end;
      end;
    end;
    

    (LAUGHS) last.) The treads can be implemented as three separate stacks. (Before, there's a special type in the pascal called "written."recordwhich may be used.)


Log in to reply
 


Suggested Topics

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