program graphomaton; const deg = 3; type pvertex = ^vertex; vertex = record s: array [1..deg] of pvertex; p: array [1..deg] of 1..deg; stop: boolean; x: 0..2; y: 0..1; { = 0 } z: 0..2; { = 0 } b: 0..3; { = 0 } c: 0..1; { = 0 } i: 1..3; { = 1 } end; procedure init(vv: pvertex); begin with vv^ do begin b := 0; c := 0; i := 1; y := 0; z := 0; end; end; procedure dump(vv: pvertex); begin with vv^ do begin write(' b=', b); write(' c=', c); write(' i=', i); write(' y=', y); write(' z=', z); end; end; procedure run(vv: pvertex); begin with vv^ do begin stop := false; c := 1-c; if (z=1) and ((s[1]^.z=2) or (s[2]^.z=2) or (s[3]^.z=2)) then z := 2 else if c=1 then case z of 0: begin if x=1 then z := 1; for i := 1 to 3 do if s[i]^.z = 1 then begin z := 1; b := i; end; if z=0 then stop := true; end; 1: begin if x=2 then y := 1; for i := 1 to 3 do if (s[i]^.y=1) and (s[i]^.b = p[i]) then y := 1; if (y=1) and (x=1) then z := 2; end; 2: stop := true; end; 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.