program graphomaton; const deg = 2; type pvertex = ^vertex; vertex = record s: array [1..deg] of pvertex; p: array [1..deg] of 1..deg; stop: boolean; x: 0..3; y: 0..1; { = 0 } a: 0..3; { = 0 } aa: 0..3; { = 0 } b: 0..3; { = 0 } c: 0..3; { = 0 } cc: 0..3; { = 0 } d: 0..3; { = 0 } end; procedure init(vv: pvertex); begin with vv^ do begin a := 0; aa := 0; b := 0; c := 0; cc := 0; d := 0; y := 0; end; end; procedure dump(vv: pvertex); begin with vv^ do begin write(' a=', a); write(' aa=', aa); write(' b=', b); write(' c=', c); write(' cc=', cc); write(' d=', d); write(' y=', y); end; end; procedure run(vv: pvertex); begin with vv^ do begin stop := false; if (x=1) and (s[1]^.x=1) then begin y:=1; stop := true; end; if x=1 then begin x:=2; d:=3; end; if s[2]^.x=1 then c:=2; if c>0 then dec(c); if s[2]^.c=1 then c:=1; if (x=2) and (c>0) then begin c:=0; cc:=2; end; if cc>0 then dec(cc); if s[1]^.cc=1 then cc:=1; if d>0 then dec(d); if s[2]^.d=1 then d:=3; if (x=0) and (cc>0) and (d>0) then begin cc:=0; d:=0; x:=3; a:=2; b:=3; end; if a>0 then dec(a); if s[1]^.a=1 then a:=1; if (x=2) and (a>0) then begin a:=0; aa:=2; end; if aa>0 then dec(aa); if s[2]^.aa=1 then aa:=1; if b>0 then dec(b); if s[1]^.b=1 then b:=2; if (x=2) and (b>0) then begin b:=0; x:=1; end; if (x=3) and (aa>0) then begin aa:=0; x:=1; end; if (a=0) and (aa=0) and (b=0) and (c=0) and (cc=0) and (d=0) and (x<>1) then stop := true; end; end; { Runtime library for the graphomaton (c) 2006 Martin Mares } const MAXN = 100; var V, Vprev: array [1..MAXN] of vertex; E: array [1..MAXN, 1..deg] of 1..deg; N: 0..MAXN; procedure read_in; var i, j, k: integer; begin read(N); for i:=1 to N do begin for j:=1 to deg do read(E[i,j]); read(V[i].x); end; for i:=1 to N do begin for j:=1 to deg do begin V[i].s[j] := @V[E[i,j]]; k := 1; while (k <= deg) and (E[E[i,j],k] <> i) do inc(k); if k > deg then begin writeln('Input inconsistent: no opposite edge for ', i, '#', j); halt(1); end; V[i].s[j] := @Vprev[E[i,j]]; V[i].p[j] := k; end; init(@V[i]); end; end; procedure dump_all; var i, j: integer; begin for i:=1 to N do begin write(i); { write(' ('); for j:=1 to deg do begin if j>1 then write(','); write(E[i,j]); end; write(')'); } write(#9, 'x=', V[i].x); dump(@V[i]); writeln(' stop=', ord(V[i].stop)); end; end; procedure iterate; var i, it, nstop: integer; begin; it := 0; writeln('### Initial configuration ###'); dump_all; repeat inc(it); nstop := 0; Vprev := V; for i := 1 to N do begin run(@V[i]); if V[i].stop then inc(nstop); end; writeln('### Iteration ', it, ' ###'); dump_all; until nstop = N; end; begin read_in; iterate; end.