Please help!
I think the way to solve this prob is convert to Dekart coordinate-system.
Below is my solution, I got WA at test #2 >.<.
var
nowX: longint;
nowY: longint;
{--------------------------------------------------------}
procedure moveX(lengths: longint);
begin
nowX:= nowX + lengths;
end;
{--------------------------------------------------------}
procedure moveY(lengths: longint);
begin
nowY:= nowY + lengths;
end;
{--------------------------------------------------------}
procedure move(section: integer; direction: char; lengths: longint);
var
tmp: longint;
begin
case direction of
'X': begin
case section of
1: if (nowX + lengths < 0) then
begin
tmp:= nowX;
moveX(-nowX);
move(2, 'X', lengths + tmp);
end
else moveX(lengths);
4: if (nowX + lengths < 0) then
begin
tmp:= nowX;
moveX(-nowX);
move(2, 'X', lengths + tmp);
end
else moveX(lengths);
2: if (nowX + lengths > 0) then
begin
tmp:= nowX;
move(2, 'X', -nowX);
move(1, 'X', lengths + tmp);
end
else begin
moveX(lengths);
moveY(lengths);
end;
3: if (nowX + lengths > 0) then
begin
tmp:= nowX;
move(3, 'X', -nowX);
move(4, 'X', lengths + tmp);
end
else begin
moveX(lengths);
moveY(lengths);
end;
end;
end;
'Y': begin
moveY(lengths);
end;
'Z': begin
case section of
1: if (nowX - lengths < 0) then
begin
tmp:= nowX;
move(1, 'Z', nowX);
move(2, 'Z', lengths - tmp);
end
else begin
moveX(-lengths);
moveY(lengths);
end;
4: if (nowY + lengths > 0) then
begin
tmp:= nowY;
move(4, 'Z', -nowY);
move(1, 'Z', lengths + tmp);
end
else begin
if (nowX - lengths < 0) then
begin
tmp:= nowX;
move(4, 'Z', nowX);
move(3, 'Z', lengths - tmp);
end
else begin
moveX(-lengths);
moveY(lengths);
end;
end;
2: if (nowX - lengths > 0) then
begin
tmp:= nowX;
move(2, 'Z', nowX);
move(1, 'Z', lengths - tmp);
end
else moveX(-lengths);
3: if (nowX - lengths > 0) then
begin
tmp:= nowX;
move(3, 'Z', nowX);
move(4, 'Z', lengths - tmp);
end
else moveX(-lengths);
end;
end;
end;
end;
{--------------------------------------------------------}
function getNowSection: integer;
begin
if (nowX < 0) then
begin
if (nowY < 0) then exit(3)
else exit(2);
end
else begin
if (nowY < 0) then exit(4)
else exit(1);
end;
end;
{--------------------------------------------------------}
procedure readInput;
var
i: integer;
n: integer;
dir: char;
len: longint;
begin
readln(n);
for i:= 1 to n do
begin
readln(dir, len);
move(getNowSection, dir, len);
end;
end;
{--------------------------------------------------------}
procedure writeOutput;
begin
if (nowX = 0) and (nowY = 0) then
begin
write(0);
exit;
end;
if (nowX = 0) then
begin
writeln(1);
write('Y ', -nowY);
exit;
end;
if (nowY = 0) then
begin
writeln(1);
if (nowX > 0) then write('X ', -nowX)
else write('Z ', nowX);
exit;
end;
writeln(2);
case getNowSection of
1: begin
writeln('X ', -nowX);
write('Y ', -nowY);
end;
2: begin
writeln('Z ', nowX);
write('Y ', -nowY);
end;
3: begin
if (nowX = nowY) then write('X ', -nowY)
else if (nowX < nowY) then
begin
writeln('X ', -nowY);
move(3, 'X', -nowY);
writeln('Z ', nowX);
end
else begin
writeln('X ', -nowX);
move(3, 'X', -nowX);
write('Y ', -nowY);
end;
end;
4: begin
if (nowX = abs(nowY)) then write('Z ', nowX)
else if (nowX < abs(nowY)) then
begin
writeln('Z ', nowX);
move(4, 'Z', nowX);
write('Y ', -nowY);
end
else begin
writeln('Z ', -nowY);
move(4, 'Z', nowY);
write('X ', -nowX);
end;
end;
end;
end;
{--------------------------------------------------------}
begin
readInput;
writeOutput;
end.