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

Discussion of Problem 1054. Tower of Hanoi

look at this AC one,it's very easy to understand!!
Posted by geniushjs 9 Aug 2003 06:32
program hanoi;
var
step:longint;
i,n:integer;
goal:array[1..31] of byte;
procedure p(k:integer;a,b,c:integer);
begin
if k=0 then exit;
if goal[k]=b then
 begin
  step:=step+trunc(exp((k-1)*ln(2)));
  p(k-1,c,b,a);
 end
else if goal[k]=c then begin writeln(-1); halt; end
else p(k-1,a,c,b);
end;
begin
readln(n);
for i:=1 to n do read(goal[i]);
step:=0;
p(n,1,2,3);
writeln(step);
end.
Re: look at this AC one,it's very easy to understand!!
Posted by Macarie programatorul in actiune 24 Aug 2004 16:42
here's mine
#include <stdio.h>

int n, s[50], s1[] = {0, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3, 1},
              s2[] = {0, 1, 3, 3, 2, 2, 1, 1, 3, 3, 2, 2, 1};


int is(int lev, long pos)
{
    if (pos % 12 == 0) pos++;
    return (lev & 1) ? s1[pos % 12] : s2[pos % 12];
}

long baga_mare(int lev, long pos)
{
    long fiu;
    if (lev == n) return pos;
    fiu = 2 * pos - 1;
    if (s[n - lev] == is(lev + 1, fiu))
        return baga_mare(lev + 1, fiu);
    fiu = 2 * pos;
    if (s[n - lev] == is(lev + 1, fiu))
        return baga_mare(lev + 1, fiu);
    return 0;
}

int main()
{
    int i;

    scanf("%d", &n);
    for (i = 1; i <= n; scanf("%d", s + i++));

    printf("%ld\n", baga_mare(0, 1) - 1);

    return 0;
}