Cod sursa(job #18687)

Utilizator girl_styleBianca Boeriu girl_style Data 18 februarie 2007 12:58:35
Problema Culori Scor 4
Compilator fpc Status done
Runda preONI 2007, Runda 2, Clasele 11-12 Marime 0.71 kb
var n,sol:byte;
    a:array[1..600] of byte;
    c:array[1..256] of byte;
    m:set of byte;

procedure citire;
var i:integer;
begin
  assign(input,'culori.in');
  reset(input);
  readln(n);
  for i:=1 to 2*n-1 do
    read(a[i]);
  close(input);
end;

procedure prel(vf,nr,t:integer);
var i:integer;
begin
  if nr=n then
    inc(sol)
              else
  begin
  c[vf]:=a[nr];
  for i:=1 to n do
    if not (i in m ) then
     if (c[i]=0) or ((nr<n) and (c[i]=a[nr+1])) then
      begin
        m:=m+[t];
        prel(i,nr+1,vf);
        m:=m-[t];
      end
  end;
end;

begin
  assign(output,'culori.out');
  rewrite(output);
  citire;
  prel(1,1,0);
  writeln(sol);
  close(output);
end.