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

What's wrong with my code here?
Posted by Li, Yi 23 Nov 2001 09:47
const
dir : array[1..4, 1..2]of shortint = ((1, 0), (-1, 0),
(0, 1), (0, -1));
left : array[1..4]of byte = (3, 4, 2, 1);
right : array[1..4]of byte = (4, 3, 1, 2);
var
a : array[-101..101, -101..101]of byte;
v : array['A'..'Z']of boolean;
i, n, m, k, d : integer;
x, y : shortint;
exp : string;
ch : char;
ans : boolean;

function leftbracket(i : integer) : integer;
var j : integer;
begin
j := 0;
repeat
if exp[i] = '(' then inc(j);
if exp[i] = ')' then dec(j);
dec(i);
until j = 0;
leftbracket := i + 1;
end;

function getopr(a, b : integer) : integer;
var i, j, k : integer;
begin
i := b;
j := 0;
while i >= a do
begin
if exp[i] = '|' then begin getopr := i; exit; end;
if exp[i] = '&' then
if j < 2 then
begin k := i; j := 2; end;
if exp[i] = '!' then
if j < 4 then
begin k := i; j := 3; end;
if exp[i] = ')' then i := leftbracket(i);
dec(i);
end;
getopr := k;
end;

function eval(a, b : integer) : boolean;
var i : integer;
l, r : boolean;
begin
if a = b then
begin
if exp[a] = 't' then eval := true
else if exp[a] = 'f' then eval := false
else eval := v[exp[a]];
exit;
end;
if (exp[a] = '(') and (exp[b] = ')') and (leftbracket(b)
= a) then
begin
eval := eval(a + 1, b - 1);
exit;
end;
i := getopr(a, b);
if exp[i] = '|' then
begin
l := eval(a, i - 1);
r := eval(i + 1, b);
eval := l or r;
end
else
if exp[i] = '&' then
begin
l := eval(a, i - 1);
r := eval(i + 1, b);
eval := l and r;
end
else
begin
l := eval(i + 1, b);
eval := not l;
end;
end;

procedure convert;
var i : integer;
begin
i := pos('OR', exp);
while i > 0 do
begin
delete(exp, i, 2);
insert('|', exp, i);
i := pos('OR', exp);
end;
i := pos('AND', exp);
while i > 0 do
begin
delete(exp, i, 3);
insert('&', exp, i);
i := pos('AND', exp);
end;
i := pos('NOT', exp);
while i > 0 do
begin
delete(exp, i, 3);
insert('!', exp, i);
i := pos('NOT', exp);
end;
i := pos('TRUE', exp);
while i > 0 do
begin
delete(exp, i, 4);
insert('t', exp, i);
i := pos('TRUE', exp);
end;
i := pos('FALSE', exp);
while i > 0 do
begin
delete(exp, i, 5);
insert('f', exp, i);
i := pos('FALSE', exp);
end;
i := pos(' ', exp);
while i > 0 do
begin
delete(exp, i, 1);
i := pos(' ', exp);
end;
end;

begin
assign(input, '1101.in'); reset(input);

for i := 1 to m do
begin
a[x, y] := 1;
end;
for i := 1 to k do
begin
while not (ch in ['A'..'Z']) do read(ch);
a[x, y] := ord(ch);
end;

close(input);

fillchar(v, sizeof(v), 0);
x := 0; y := 0; d := 1;
repeat
writeln(x, ' ', y);
x := x + dir[d, 1];
y := y + dir[d, 2];
if a[x, y] = 1 then
begin
ans := eval(1, length(exp));
if ans then d := right[d] else d := left[d];
end
else
if a[x, y] > 60 then
v[chr(a[x, y])] := not v[chr(a[x, y])];
until (abs(x) > n) or (abs(y) > n);
end.
Re: What's wrong with my code here?
Posted by I have answers to all your questions :) 23 Nov 2001 16:27
there's a bug in getopr function
What is the bug?
Posted by Li, Yi 23 Nov 2001 16:47
> there's a bug in getopr function
Re: What is the bug?
Posted by I have answers to all your questions :) 23 Nov 2001 18:26
change 4 numbers in getopr function and u'll get accepted ;)
HINT :
if exp = 'NOT FALSE AND FALSE'
ur program evaluate it this way : NOT (FALSE AND FALSE)
I modified my code and WA again.
Posted by Li, Yi 24 Nov 2001 15:54
Please say how to fix the bug clearly.
modified getopr function
Posted by I have answers to all your questions :) 24 Nov 2001 17:12
function getopr(a, b : integer) : integer;
var i, j, k : integer;
begin
i := b;
j := 0;
while i >= a do
begin
if exp[i] = '|' then begin getopr := i; exit; end;
if exp[i] = '&' then
if j < 4 then
begin k := i; j := 3; end;
if exp[i] = '!' then
if j < 2 then
begin k := i; j := 2; end;
if exp[i] = ')' then i := leftbracket(i);
dec(i);
end;
getopr := k;
end;
I modified it just as this! I forgot remove file operation lines, so WA. Thank you
Posted by Li, Yi 24 Nov 2001 17:18
> function getopr(a, b : integer) : integer;
> var i, j, k : integer;
> begin
>   i := b;
>   j := 0;
>   while i >= a do
>   begin
>     if exp[i] = '|' then begin getopr := i; exit; end;
>     if exp[i] = '&' then
>       if j < 4 then
>         begin k := i; j := 3; end;
>     if exp[i] = '!' then
>       if j < 2 then
>         begin k := i; j := 2; end;
>     if exp[i] = ')' then i := leftbracket(i);
>     dec(i);
>   end;
>   getopr := k;
> end;