- 注册时间
- 2008-7-21
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 11556
- 在线时间
- 小时
|
发表于 2009-2-25 09:53:42
|
显示全部楼层
- program liangshui;
- const
- max=600000;
- type
- tlist=record {结点类型}
- father:longint;
- dep:byte;
- a:integer;
- b:integer;
- end;
- var
- list:array[0..max] of tlist; {扩展出的中间结点序列}
- head,foot,best,i:longint;
- m,n,k:integer;
- answer:longint;
- found:boolean;
- procedure init; {初始化过程}
- var
- i:byte;
- begin
- assign(input,'ls.in');
- reset(input);
- assign(output,'ls.out');
- rewrite(output);
- fillchar(list,sizeof(list),0);
- found:=false;
- head:=0; {队列初始化,队首指针head,队尾指针foot}
- foot:=1;
- with list[1] do {初始结点作为队列第一个结点}
- begin
- a:=0;
- b:=0;
- dep:=0;
- father:=0;
- end;
- readln(m,n,k)
- end;
- function notappear(newv:tlist):boolean; {判断扩展出的结点是否已在队列中的函数}
- var
- i:longint;
- begin
- notappear:=false;
- for i:=1 to foot do
- if (newv.a=list[i].a) and (newv.b=list[i].b)
- then exit;
- notappear:=true;
- end;
- procedure add(newv:tlist); {往队列中加入新结点过程}
- begin
- if notappear(newv)
- then begin
- inc(foot);
- list[foot]:=newv;
- end;
- end;
- procedure expand(index:longint;var oldv:tlist); {扩展结点过程}
- var
- i:integer;
- newv:tlist;
- begin
- for i:=1 to 6 do {分别应用6条规则}
- begin
- if i=1 then
- if oldv.a<>0 {把a量筒倒空}
- then begin newv.a:=0;newv.b:=oldv.b;end;
- if i=2 then
- if oldv.a<>m {把a量筒灌满}
- then begin newv.a:=m;newv.b:=oldv.b;end;
- if i=3 then
- if oldv.b<>0 {把b量筒倒空}
- then begin newv.b:=0;newv.a:=oldv.a;end;
- if i=4 then
- if oldv.a<>n {把b量筒灌满}
- then begin newv.a:=n;newv.a:=oldv.a;end;
- if i=5 then
- if (oldv.a<>0) and (oldv.b<>n) {把a量筒往b量筒倒水}
- then if oldv.a+oldv.b>=n {判断a往b倒时b能否全部装下}
- then begin newv.b:=n;newv.a:=oldv.a-(n-oldv.b);end
- else begin newv.a:=0;newv.b:=oldv.a+oldv.b;end;
- if i=6 then
- if (oldv.a<>m) and (oldv.b<>0) {把b量筒往a量筒倒水}
- then if oldv.a+oldv.b>=m {判断b往a倒时a能否全部装下}
- then begin newv.a:=m;newv.b:=oldv.b-(m-oldv.a);end
- else begin newv.b:=0;newv.a:=oldv.a+oldv.b;end;
- newv.father:=index;
- newv.dep:=oldv.dep+1;
- add(newv);
- end;
- end;
- procedure print(index:longint); {递归打印路径}
- var
- i,j:byte;
- begin
- if index=0 then exit;
- print(list[index].father);
- writeln(list[index].a,' ',list[index].b);
- end;
- begin{main}
- init;
- repeat
- inc(head);
- if list[head].a=k {比较是否跟目标相同,相同则找到,否则扩展新结点}
- then begin
- found:=true;
- best:=list[head].dep;
- answer:=head;
- break;
- end;
- if list[foot].dep>100
- then begin
- writeln('OverTime!');
- break;
- end;
- expand(head,list[head]);
- {writeln(list[head].a,' ',list[head].b);}
- until (head>=foot) or (foot>max) or found;
- if found
- then begin
- writeln(best);
- print(answer);
- end
- else writeln('No Answer');
- close(input);
- close(output);
- end.
-
复制代码 |
|