| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
贴个PASCAL的代码。。。主程序比较短。var
a,b,c,i,j,head,tail,x,y,ji:longint;
q:array[0..1000000,1..4] of longint;
v:array[0..101,0..101] of boolean;
procedure print(x:longint);
begin
inc(ji);
if q[x,3]=0 then begin writeln(ji-1); exit; end;
print(q[x,3]);
case q[x,4] of
1:writeln('FILL(1)');
2:writeln('FILL(2)');
3:writeln('DROP(1)');
4:writeln('DROP(2)');
5:writeln('POUR(1,2)');
6:writeln('POUR(2,1)');
end;
end;
function canwork(i:longint):boolean;
begin
canwork:=false;
x:=q[head,1]; y:=q[head,2];
case i of
1:if x<a then exit(true);
2:if y<b then exit(true);
3:if x>0 then exit(true);
4:if y>0 then exit(true);
5:if (x>0) and (y<b) then exit(true);
6:if (y>0) and (x<a) then exit(true);
end;
end;
procedure work(i:longint);
begin
x:=q[head,1]; y:=q[head,2];
case i of
1:x:=a;
2:y:=b;
3:x:=0;
4:y:=0;
5:begin y:=y+x; if y>b then begin x:=y-b; y:=b; end else x:=0; end;
6:begin x:=x+y; if x>a then begin y:=x-a; x:=a; end else y:=0; end;
end;
if not v[x,y] then
begin
v[x,y]:=true;
inc(tail);
q[tail,1]:=x;
q[tail,2]:=y;
q[tail,3]:=head;
q[tail,4]:=i;
end;
end;
{MAIN}
begin
readln(a,b,c);
head:=1; tail:=1;
q[1,1]:=0; q[1,2]:=0; q[1,3]:=0; q[1,4]:=0;
v[0,0]:=true;
while head<=tail do
begin
for i:=1 to 6 do
begin
if canwork(i) then work(i);
if (q[tail,1]=c) or (q[tail,2]=c) then
begin
ji:=0; print(tail); halt;
end;
end;
inc(head);
end;
writeln('impossible');
end.
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator