COLLECT - VOI05 Bộ sưu tập

Tác giả: RR

Ngôn ngữ: Pascal

{$R+,Q+}
const
  FINP='';
  FOUT='';
  MAXN=1000;
  oo=200;
var
  d:array[0..4,0..4,0..4] of longint;
  first,last,z0,m0,s0,z,m,s,n,sl:longint;
  qz,qm,qs:array[1..200] of longint;
  z1,m1,s1,z2,m2,s2:array[1..1000000] of longint;
procedure inp;
var
  f:text;
begin
  assign(f,FINP); reset(f);
  read(f,n);
  read(f,z,s,m); read(f,z0,s0,m0);
  sl:=0;
  while not eof(f) do
    begin
      inc(sl);
      read(f,z1[sl],s1[sl],m1[sl]);
      read(f,z2[sl],s2[sl],m2[sl]);
    end;
  close(f);
end;
procedure ans;
var
  f:text;
  i,j,k,count:longint;
begin
  assign(f,FOUT); rewrite(f);
  count:=0;
  for i:=z0 to 4 do
  for j:=s0 to 4 do
  for k:=m0 to 4 do
    if d[i,j,k]<oo then inc(count);
  if count=0 then
    begin
      writeln(f,-1);
      close(f);
      halt;
    end;
  writeln(f,count);
  for i:=z0 to 4 do
  for j:=s0 to 4 do
  for k:=m0 to 4 do
    if d[i,j,k]<oo then
      writeln(f,i,' ',j,' ',k,' ',d[i,j,k]);
  close(f);
end;
procedure solve;
var
  i,j,k,ii,jj,kk,u:longint;
begin
  for i:=0 to 4 do
  for j:=0 to 4 do
  for k:=0 to 4 do
    d[i,j,k]:=oo;
  first:=1; last:=1;
  qz[1]:=z; qs[1]:=s; qm[1]:=m;
  d[z,s,m]:=0;
  while first<=last do
    begin
      i:=qz[first]; j:=qs[first]; k:=qm[first]; inc(first);
      if d[i,j,k]=n then exit;
      for u:=1 to sl do
      if (i>=z1[u]) and (j>=s1[u]) and (k>=m1[u]) then
        begin
          ii:=i-z1[u]+z2[u];
          jj:=j-s1[u]+s2[u];
          kk:=k-m1[u]+m2[u];
          if (ii<=4) and (jj<=4) and (kk<=4) and (d[ii,jj,kk]=oo) then
            begin
              d[ii,jj,kk]:=d[i,j,k]+1;
              if (ii<z0) or (jj<s0) or (kk<m0) then
                begin
                  inc(last);
                  qz[last]:=ii;
                  qs[last]:=jj;
                  qm[last]:=kk;
                end;
            end;
        end;
    end;
end;
begin
  inp;
  solve;
  ans;
end.

Download