ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1101. Robot in the Field

Can onyone give me a test. I get Output Limit (+)
Posted by Andrey Popyk (popyk@ief.tup.km.ua) 15 Mar 2002 19:00
Can onyone tell me, where my programm fails.
My programm gets Output Limit, and I don't know where it wrong.
I convert Boolean expression into a postfix form and simply simulate
a robot movement.

CONST Dim = 10007;
Dim2 = 101;

TYPE TBuf = Array[0..Dim] of char;
VAR Buf,Stack:TBuf;
BST:Array[0..3007] of boolean;
BP,SP:integer;
CurrCh:char;
F:Array[-Dim2..Dim2,-Dim2..Dim2] of char;
Reg:Array['A'..'\'] of boolean;
N:integer;

FUNCTION GetLexem:char;
var S:string;
begin
S:='';
while not (CurrCh in ['A'..'Z','(',')',#13]) do read(CurrCh);
if CurrCh=#13 then begin GetLexem:=#13; Exit end;
if (CurrCh='(') or (CurrCh=')') then begin GetLexem:=CurrCh; read
(CurrCh); Exit end;
while CurrCh in ['A'..'Z'] do
if S='NOT' then S:='!';
if S='AND' then S:='&';
if S='OR' then S:='|';
if S='TRUE' then S:='[';
if S='FALSE' then S:='\';
GetLexem:=S[1];
if length(S)>1 then begin writeln('Error!'); Halt(0) end;
end;

begin
inc(BP); Buf[BP]:=C;
end;

FUNCTION Prior(C:char):integer;
begin
case C of
'(':Prior:=0;
'|':Prior:=1;
'&':Prior:=2;
'!':Prior:=3;
end;
end;

var P:integer;
begin
if C='(' then begin inc(SP); Stack[SP]:='('; Exit end;
if C=')' then begin
while Stack[SP]<>'(' do begin AddBuf(Stack[SP]); dec
(SP) end;
dec(SP);
Exit;
end;
P:=Prior(C);
while (SP>0) and (P<=Prior(Stack[SP])) do
begin
dec(SP);
end;
inc(SP); Stack[SP]:=C;
end;

PROCEDURE ConvertProgram;
var C:char;
begin
BP:=0; SP:=0;
CurrCh:=#0;
repeat
C:=GetLexem;
if C=#13 then break;
until C=#13;
while SP>0 do begin AddBuf(Stack[SP]); dec(SP) end;
end;

var M,K,R,C,i:integer;
ch1,ch2:char;
begin
for i:=1 to M do
begin
F[R,C]:='#';
end;
for i:=1 to K do
begin
F[R,C]:=ch2;
end;
end;

PROCEDURE TurnRight(var DR,DC:integer);
begin
if Abs(DR)=1 then begin DC:=-DR; DR:=0 end
else begin DR:=DC; DC:=0 end;
end;

PROCEDURE TurnLeft(var DR,DC:integer);
begin
if Abs(DR)=1 then begin DC:=DR; DR:=0 end
else begin DR:=-DC; DC:=0 end;
end;

FUNCTION Count:boolean;
var BSP,i:integer;
begin
BSP:=0;
for i:=1 to BP do
begin
if Buf[i] in ['A'..'\']
then begin inc(BSP); BST[BSP]:=Reg[Buf[i]] end
else begin
if Buf[i]='!' then BST[BSP]:=not BST[BSP];
if Buf[i]='&' then begin BST[BSP]:=BST[BSP-1] and BST
[BSP]; dec(BSP) end;
if Buf[i]='|' then begin BST[BSP]:=BST[BSP-1] or BST
[BSP]; dec(BSP) end;
end;
end;
Count:=BST[BSP];
if BSP<>1 then begin writeln('Error!'); Halt(0) end;
end;

PROCEDURE Simulate;
var ch:char;
R,C,DR,DC:integer;
begin
for ch:='A' to '\' do Reg[ch]:=false;
Reg['[']:=true;
R:=0; C:=0; DR:=1; DC:=0;
if F[0,0]<>#0 then Reg[F[0,0]]:=not Reg[F[0,0]];
repeat
writeln(R,' ',C);
R:=R+DR; C:=C+DC;
if (Abs(R)>N) or (Abs(C)>N) then break;
if F[R,C]='#' then begin
if Count then TurnRight(DR,DC)
else TurnLeft(DR,DC);
end
else if F[R,C]<>#0 then Reg[F[R,C]]:=not Reg[F
[R,C]];
until false;
end;

BEGIN
{ assign(INPUT,'robot.dat'); reset(INPUT);}

ConvertProgram;
Simulate;
END.
Ok, just COUNT wrong.
Posted by Andrey Popyk (popyk@ief.tup.km.ua) 20 Mar 2002 11:56
> Can onyone tell me, where my programm fails.
> My programm gets Output Limit, and I don't know where it wrong.
> I convert Boolean expression into a postfix form and simply
simulate
> a robot movement.
>
> CONST Dim = 10007;
>       Dim2 = 101;
>
> TYPE TBuf = Array[0..Dim] of char;
> VAR Buf,Stack:TBuf;
>     BST:Array[0..3007] of boolean;
>     BP,SP:integer;
>     CurrCh:char;
>     F:Array[-Dim2..Dim2,-Dim2..Dim2] of char;
>     Reg:Array['A'..'\'] of boolean;
>     N:integer;
>
> FUNCTION GetLexem:char;
> var S:string;
> begin
>   S:='';
>   while not (CurrCh in ['A'..'Z','(',')',#13]) do read(CurrCh);
>   if CurrCh=#13 then begin GetLexem:=#13; Exit end;
>   if (CurrCh='(') or (CurrCh=')') then begin GetLexem:=CurrCh; read
> (CurrCh); Exit end;
>   while CurrCh in ['A'..'Z'] do
>   if S='NOT' then S:='!';
>   if S='AND' then S:='&';
>   if S='OR' then S:='|';
>   if S='TRUE' then S:='[';
>   if S='FALSE' then S:='\';
>   GetLexem:=S[1];
>   if length(S)>1 then begin writeln('Error!'); Halt(0) end;
> end;
>
> begin
>   inc(BP); Buf[BP]:=C;
> end;
>
> FUNCTION Prior(C:char):integer;
> begin
>   case C of
>     '(':Prior:=0;
>     '|':Prior:=1;
>     '&':Prior:=2;
>     '!':Prior:=3;
>   end;
> end;
>
> var P:integer;
> begin
>   if C='(' then begin inc(SP); Stack[SP]:='('; Exit end;
>   if C=')' then begin
>                   while Stack[SP]<>'(' do begin AddBuf(Stack[SP]);
dec
> (SP) end;
>                   dec(SP);
>                   Exit;
>                 end;
>   P:=Prior(C);
>   while (SP>0) and (P<=Prior(Stack[SP])) do
>   begin
>     dec(SP);
>   end;
>   inc(SP); Stack[SP]:=C;
> end;
>
> PROCEDURE ConvertProgram;
> var C:char;
> begin
>   BP:=0; SP:=0;
>   CurrCh:=#0;
>   repeat
>     C:=GetLexem;
>     if C=#13 then break;
>   until C=#13;
>   while SP>0 do begin AddBuf(Stack[SP]); dec(SP) end;
> end;
>
> var M,K,R,C,i:integer;
>     ch1,ch2:char;
> begin
>   for i:=1 to M do
>   begin
>     F[R,C]:='#';
>   end;
>   for i:=1 to K do
>   begin
>     F[R,C]:=ch2;
>   end;
> end;
>
> PROCEDURE TurnRight(var DR,DC:integer);
> begin
>   if Abs(DR)=1 then begin DC:=-DR; DR:=0 end
>                else begin DR:=DC; DC:=0 end;
> end;
>
> PROCEDURE TurnLeft(var DR,DC:integer);
> begin
>   if Abs(DR)=1 then begin DC:=DR; DR:=0 end
>                else begin DR:=-DC; DC:=0 end;
> end;
>
> FUNCTION Count:boolean;
> var BSP,i:integer;
> begin
>   BSP:=0;
>   for i:=1 to BP do
>   begin
>     if Buf[i] in ['A'..'\']
>       then begin inc(BSP); BST[BSP]:=Reg[Buf[i]] end
>       else begin
>              if Buf[i]='!' then BST[BSP]:=not BST[BSP];
>              if Buf[i]=