ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1122. Game

why WA?
Posted by wangchun 21 Jul 2003 13:21
type table=array[1..4,1..4]of byte;
var sz,back:table;
    turn:array[-1..1,-1..1]of byte;
    out:shortint;
procedure init;
var i,j:shortint;
    c:char;
begin
     out:=-1;
     fillchar(sz,sizeof(sz),0);
     fillchar(back,sizeof(back),0);
     fillchar(turn,sizeof(turn),0);
     for i:=1 to 4 do
         begin
              for j:=1 to 4 do
                  begin
                       read(c);
                       while not (c in['B','W']) do
                             read(c);
                       if c='B' then
                          sz[i,j]:=1
                                else
                          sz[i,j]:=0
                  end;
              readln;
         end;
     back:=sz;
     for i:=-1 to 1 do
         begin
              for j:=-1 to 1 do
                  begin
                       read(c);
                       while not (c in['1','0']) do
                             read(c);
                       if c='1' then
                          turn[i,j]:=1
                                else
                          turn[i,j]:=0;
                  end;
              readln;
         end;
end;
procedure mark(a,b:byte);
var i,j:shortint;
begin
     for i:=-1 to 1 do
         for j:=-1 to 1 do
             if ((a+i) in[1..4])and((b+j) in [1..4]) then
                if turn[i,j]=1 then
                   sz[a+i,b+j]:=(sz[a+i,b+j]+1)mod 2;
end;
procedure make;
var s:table;
    t:word;
    i,j,k,l:byte;
    q:boolean;
begin
     for t:=0 to 65535 do
         begin
              fillchar(s,sizeof(s),0);
              i:=t;
              for k:=0 to 15 do
                  begin
                       l:=i mod 2;
                       i:=i div 2;
                       s[k div 4+1,k mod 4+1]:=l;
                  end;
              for i:=1 to 4 do
                  for j:=1 to 4 do
                      if s[i,j]=1 then
                         mark(i,j);
              q:=true;
              k:=sz[1,1];
              for i:=1 to 4 do
                  begin
                  for j:=1 to 4 do
                      if sz[i,j]<>k then
                         begin
                              q:=false;
                              break;
                         end;
                  if not q then
                     break;
                  end;
              if q then
                 begin
                      k:=0;
                      for i:=1 to 4 do
                          for j:=1 to 4 do
                              if s[i,j]<>0 then
                                 inc(k);
                      if (k<out)or(out=-1) then
                         out:=k;
                 end;
              sz:=back;
         end;
end;
procedure print;
begin
     if out=-1 then
        writeln('Impossible')
               else
        writeln(out);
end;
begin
     init;
     make;
     print;
end.