ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1054. Ханойская башня

A simple problem-> a impossible solution
Послано Simeon Kostenski 17 апр 2001 03:15
Can anyone of those who have solved the problem tell me
how ? If you use recursion 31 is a extremely enormous limit
and it works years. So a formula? How?
use the formula given in the problem ;) (+)
Послано Dinh Quang Hiep (mg9h@yahoo.com) 19 апр 2001 14:18
it takes 2^k-1 steps to move k disk from one peg to another

QH@
Re: A simple problem-> a impossible solution
Послано Timus Observer 26 авг 2001 07:22
> Can anyone of those who have solved the problem tell me
> how ? If you use recursion 31 is a extremely enormous
limit
> and it works years. So a formula? How?
I am facing the same problem. Time limite exceeding. Could
somone advise me how to optimize the algorithm ?.program
hanoit;
const maxN=31;
var i,n,nmove:integer;
    current,target:array[1..maxN] of integer;

function sama:boolean;
var i:integer;
begin
  sama:=true;
  for i:=1 to N do if current[i]<>target[i] then
  begin
    sama:=false;
    exit;
  end;
end;

procedure move(n,to_:integer);
begin
  inc(nmove);
  current[n]:=to_;
  if sama then begin writeln(nmove); readln; halt; end;
end;

procedure hanoi (n, from, to_, temp : integer);
begin
  if n > 0 then
  begin
    hanoi (n-1,from,temp,to_);
    move(n,to_);
    hanoi(n-1,temp,to_,from);
  end;
end;
begin
  nmove:=0;
  read(N);
  for i:=1 to N do
  begin
    read(target[i]);
    current[i]:=1;
  end;
  if sama then begin writeln(nmove); readln; halt; end
  else hanoi(N,1,2,3);
  writeln(-1 );
end.
Re: A simple problem-> a impossible solution
Послано Dan Ghinea 7 янв 2002 04:18
First of all you shouldn't use readln after you print the
solution.It'll wait forever!

Good luck!

> I am facing the same problem. Time limite exceeding. Could
> somone advise me how to optimize the algorithm ?.program
> hanoit;
> const maxN=31;
> var i,n,nmove:integer;
>     current,target:array[1..maxN] of integer;
>
> function sama:boolean;
> var i:integer;
> begin
>   sama:=true;
>   for i:=1 to N do if current[i]<>target[i] then
>   begin
>     sama:=false;
>     exit;
>   end;
> end;
>
> procedure move(n,to_:integer);
> begin
>   inc(nmove);
>   current[n]:=to_;
>   if sama then begin writeln(nmove); readln; halt; end;
> end;
>
> procedure hanoi (n, from, to_, temp : integer);
> begin
>   if n > 0 then
>   begin
>     hanoi (n-1,from,temp,to_);
>     move(n,to_);
>     hanoi(n-1,temp,to_,from);
>   end;
> end;
> begin
>   nmove:=0;
>   read(N);
>   for i:=1 to N do
>   begin
>     read(target[i]);
>     current[i]:=1;
>   end;
>   if sama then begin writeln(nmove); readln; halt; end
>   else hanoi(N,1,2,3);
>   writeln(-1 );
> end.
>
>
Re: A simple problem-> a impossible solution
Послано Chen Xiaoke 2 фев 2002 15:26
faint,of course can't do it in this way...you should find a formula...


> > Can anyone of those who have solved the problem tell me
> > how ? If you use recursion 31 is a extremely enormous
> limit
> > and it works years. So a formula? How?
> I am facing the same problem. Time limite exceeding. Could
> somone advise me how to optimize the algorithm ?.program
> hanoit;
> const maxN=31;
> var i,n,nmove:integer;
>     current,target:array[1..maxN] of integer;
>
> function sama:boolean;
> var i:integer;
> begin
>   sama:=true;
>   for i:=1 to N do if current[i]<>target[i] then
>   begin
>     sama:=false;
>     exit;
>   end;
> end;
>
> procedure move(n,to_:integer);
> begin
>   inc(nmove);
>   current[n]:=to_;
>   if sama then begin writeln(nmove); readln; halt; end;
> end;
>
> procedure hanoi (n, from, to_, temp : integer);
> begin
>   if n > 0 then
>   begin
>     hanoi (n-1,from,temp,to_);
>     move(n,to_);
>     hanoi(n-1,temp,to_,from);
>   end;
> end;
> begin
>   nmove:=0;
>   read(N);
>   for i:=1 to N do
>   begin
>     read(target[i]);
>     current[i]:=1;
>   end;
>   if sama then begin writeln(nmove); readln; halt; end
>   else hanoi(N,1,2,3);
>   writeln(-1 );
> end.
>
>