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

Общий форум

Need advise on Hanoi Tower
Послано Timus Observer 26 авг 2001 08:01
Dear All, I am a beginner in programming and need
assistance. Could someone advise me why I keep
getting 'time limit exeed' in my Hanoi Tower program below
(problem 1054) ?. How to optimize it.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: Need advise on Hanoi Tower
Послано Krzysztof Kapuscik 26 авг 2001 14:55
You have no chance to get it accepted by simulation. Try to
move whole stacks of rings to another position (there is a
hint in one of the previous messages). A bit of imagination
is needed (and mathematics of course). If you need more
assistance then write to: saveman@zeus.polsl.gliwice.pl
Good luck.
SAV