{School Name : "Dr. I. Mesota" National College

Name : Ceanga Andrei

SChool Code : 7156

Division : Senior 3

Contest : #2}

program sudoku;

uses crt;

const mult=[1,2,3,4,5,6,7,8,9];

type multime=set of 1..9;

var a:array[1..9,1..9] of 1..9;

i,j,q,x,k,nr:integer;

f:text;

r:array[1..5] of integer;

l,c,p,s:multime;

function stop(x:byte):boolean;

var y:integer;

begin

  stop:=true;

  for y:=1 to 9 do if a[x,y]=0 then stop:=false;

end;

begin

  clrscr;

  assign(f,'sudoku.txt');

  reset(f);

  for i:=1 to 9 do

    begin

      for j:=1 to 9 do read(f,a[i,j]);

      readln(f);

    end;

  for i:=1 to 5 do read(f,r[i]);

  close(f);

  for q:=1 to 5 do

    begin

    repeat

      x:=r[q];

      l:=[];

      for i:=1 to 9 do l:=l+[a[x,i]];

      l:=mult-l;

      for i:=1 to 9 do

        if a[x,i]=0 then

          begin

            c:=[];

            for j:=1 to 9 do c:=c+[a[j,i]];

            c:=mult-c;

            p:=[];

            for j:=( (x-1) div 3 )*3 to (((x-1) div 3)*3)+2 do

              for k:=((i-1)div 3)*3 to (((i-1)div 3)*3)+2 do

                p:=p+[a[j+1,k+1]];

            p:=mult-p;

            s:=l*c;

            s:=s*p;

            nr:=0;

            for j:=1 to 9 do if j in s then inc(nr);

            if nr=1 then for j:=1 to 9 do if j in s then a[x,i]:=j;

            l:=l-[a[x,i]];

          end;

      nr:=0;

      for k:=1 to 9 do if k in l then inc(nr);

      for j:=1 to 9 do if (a[x,j]=0) and (nr=1) then

        for k:=1 to 9 do if k in l then a[x,j]:=k;

    until stop(x);

      for j:=1 to 9 do write(a[x,j]);

      writeln;

    end;

  readln;

end.