ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

Need advise on Hanoi Tower
Posted by Timus Observer 26 Aug 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
Posted by Krzysztof Kapuscik 26 Aug 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