最优程序

View Code
  1 program  zuiyou;
  2 const
  3   maxl=1000;
  4 type
  5      sta=record
  6            m:array[1..5]of longint;
  7            last,done,num,la:longint;
  8          end;
  9 //sta.la为了记录搜索序列,方便后来打印,sta.last模拟数据栈,sta.num表示当前数据栈前面有多少数据;sta.done表示生成当前状态所用方案
 10 var
 11      q:array[1..maxl]of sta;
 12      b:array[1..5]of longint;
 13      head,tail,num:longint;
 14      i,n:longint;
 15      now,new:sta;
 16      flag:boolean;
 17 
 18 procedure print(k:longint);
 19 var
 20   i:longint;
 21 begin
 22 //递归输出方案
 23      if q[k].la<>q[q[k].la].la then print(q[k].la);
 24      if q[k].done=1 then write('add ');
 25      if q[k].done=2 then write('sub ');
 26      if q[k].done=3 then write('mul ');
 27      if q[k].done=4 then write('div ');
 28      if q[k].done=5 then write('dup ');
 29 end;
 30 
 31 function getans(now:sta):boolean;
 32 var
 33   i,j:longint;
 34 begin
 35      getans:=true;
 36      for i:=1 to n do
 37       if now.m[i]<>b[i] then exit(false);
 38 end;
 39 //是否可以进行除法运算
 40 function candiv(now:sta):boolean;
 41 var
 42   i:longint;
 43 begin
 44 
 45      candiv:=true;
 46      if now.num<=0 then exit(false);
 47      for i:=1 to n do
 48      if now.m[i]=0 then exit(false);
 49 end;
 50 
 51 begin
 52       assign(input,'win.in');
 53       assign(output,'win.out');
 54       reset(input);
 55       rewrite(output);
 56 
 57       //initialization
 58       fillchar(q,sizeof(q),0);
 59 
 60       readln(n);
 61       for i:=1 to n do read(q[1].m[i]); readln;
 62       for i:=1 to n do read(b[i]);readln;
 63       q[1].last:=1;
 64       q[1].la:=1;
 65 
 66       head:=0;tail:=1;
 67       while head<tail do
 68        begin
 69            inc(head);
 70            now:=q[head];
 71            //can add and sub
 72            if now.num>=1 then
 73             begin
 74                  //add
 75                  new:=now;
 76                  new.done:=1; flag:=false;
 77                  new.num:=now.num-1;
 78                  if new.num>0 then new.last:=q[now.last].last
 79                   else new.last:=tail+1;
 80                  new.la:=head;
 81                     //jian yan new shi sou man zu tiao jian
 82                  for i:=1 to n do
 83                    begin
 84                       new.m[i]:=now.m[i]+q[now.last].m[i];
 85                       if new.m[i]>3000 then
 86                        begin
 87                            flag:=true;
 88                            break;
 89                        end;
 90                    end;
 91 
 92                  if not flag then begin
 93                    inc(tail); q[tail]:=new;
 94                    if getans(new) then begin print(tail); writeln;halt;end;
 95                  end;
 96 
 97                  //sub
 98                  new:=now;
 99                  new.done:=2;  flag:=false;
100                  new.num:=now.num-1;
101                  if new.num>0 then new.last:=q[now.last].last
102                               else new.last:=tail+1;
103                  new.la:=head;
104                  flag:=false;
105 
106                  for i:=1 to n do
107                    begin
108                      new.m[i]:=q[now.last].m[i]-now.m[i];
109                      if new.m[i]>3000 then
110                        begin
111                            flag:=true;
112                            break;
113                        end;
114                    end;
115 
116                      if not flag then begin
117                        inc(tail); q[tail]:=new;
118                        if getans(new) then begin print(tail);writeln;halt;end;
119                      end;
120                  //mul
121                  new:=now; flag:=false;
122                  new.done:=3;
123                  new.num:=now.num-1;
124                  if new.num>0 then new.last:=q[now.last].last
125                               else new.last:=tail+1;
126                  new.la:=head;
127 
128                  for i:=1 to n do
129                   begin
130                    new.m[i]:=q[now.last].m[i]* now.m[i];
131                    if new.m[i]>30000 then begin flag:=true; break;end;
132                   end;
133 
134                      if not flag then begin
135                        inc(tail); q[tail]:=new;
136                        if getans(new) then begin print(tail); writeln;halt;end;
137                      end;
138 
139             end;
140 
141 
142             if candiv(now) then
143              begin
144                  new:=now;
145                  new.done:=4;
146                  new.num:=now.num-1;
147                  if new.num>0 then new.last:=q[now.last].last
148                   else new.last:=tail+1;
149                  for i:=1 to n do new.m[i]:=q[now.last].m[i]div now.m[i];
150 
151                  inc(tail); q[tail]:=new;
152                  if getans(new) then begin print(tail);  writeln;halt;end;
153              end;
154 
155              //dup
156                  new:=now;
157                  new.done:=5;
158                  new.num:=now.num+1;
159                  new.last:=head;
160                  new.la:=head;
161                  inc(tail); q[tail]:=new;
162        end;
163 
164        close(input);
165        close(output);
166 end.

最优程序

算法艺术与信息学竞赛  162页

posted @ 2012-05-23 13:23  翱翔的感觉  阅读(121)  评论(0)    收藏  举报