今天做了一道题,打表,花了几个小时,当然最后0.1秒AC。
【题目描述】【大意】
有一个花园,可以看作3*3的格子
花园中有四种花,分别是Lv1,Lv2,Lv3,Lv4,每种花都有其价值(不保证单调递增)
Lv1的花可以任意种;
Lv2的花必须在其相邻格子中种有Lv1的花时才能种;
Lv3的花必须在其相邻格子中种有Lv1和Lv2的花才能种;
Lv4的花必须在其相邻格子中种有Lv1,Lv2,Lv3的花才能种;
花可以铲除后在种,铲除不花费时间。
给n组数据,每组数据含b1,b2,b3,b4,minv,表示bi表示lvi的花的价值,minv表示最短距离。
求最短需要多少时间,使得花的总价值大于minv。
我的方法是先搜索出所有的状态,即lvi的花有ai种,其总耗时为time
然后打出表,带进另外一个程序,直接枚举每种状态即可
由于状态数只有340种,所以最后耗时0.1秒。
搜索程序:
program garden_pre;
const tx:array[1..4]of integer=(1,-1,0,0); ty:array[1..4]of integer=(0,0,1,-1);
type
gardentype=record
a:array[1..3,1..3]of byte;
b:array[1..3,1..3]of integer;
step:longint;
end;
var
d:array[1..1000000]of gardentype;
w:array[1..4]of longint; wmin:longint;
hash:array[0..4,0..4,0..4,0..4,0..4,0..4,0..4,0..4,0..4]of boolean;
t:array[0..9,0..8,0..6,0..4]of longint;
head,tail,i,j,k,l:longint;
procedure decs(i,j,s:byte; var now:gardentype; add:integer);
var t:byte;
begin
for t:=1 to 4 do
if (i+tx[t] in [1..3])and(j+ty[t] in [1..3]) then
case add of
-1:dec(now.b[i+tx[t]][j+ty[t]],trunc(exp((s-1)*ln(10))));
1:inc(now.b[i+tx[t]][j+ty[t]],trunc(exp((s-1)*ln(10))));
end;
end;
procedure simp(now:gardentype);
var tx:array[0..4]of integer;
begin
fillchar(tx,sizeof(tx),0);
with now do
for i:=1 to 3 do
for j:=1 to 3 do
inc(tx[a[i,j]]);
if (now.step<t[tx[1],tx[2],tx[3],tx[4]]) then t[tx[1],tx[2],tx[3],tx[4]]:=now.step;
end;
procedure expand(now:gardentype);
var i,j:longint;
begin
for i:=1 to 3 do
for j:=1 to 3 do
begin
inc(head); d[head]:=now; inc(d[head].step); d[head].a[i,j]:=1;
with d[head] do
if not hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]] then
begin
hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]]:=true;
decs(i,j,now.a[i,j],d[head],-1);
decs(i,j,1,d[head],1);
simp(d[head]);
end else dec(head);
if now.b[i,j] mod 10<=0 then continue;
inc(head); d[head]:=now; inc(d[head].step); d[head].a[i,j]:=2;
with d[head] do
if not hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]] then
begin
hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]]:=true;
decs(i,j,now.a[i,j],d[head],-1);
decs(i,j,2,d[head],1);
simp(d[head]);
end else dec(head);
if now.b[i,j] mod 100<=10 then continue;
inc(head); d[head]:=now; inc(d[head].step); d[head].a[i,j]:=3;
with d[head] do
if not hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]] then
begin
hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]]:=true;
decs(i,j,now.a[i,j],d[head],-1);
decs(i,j,3,d[head],1);
simp(d[head]);
end else dec(head);
if now.b[i,j] mod 1000<=100 then continue;
inc(head); d[head]:=now; inc(d[head].step); d[head].a[i,j]:=4;
with d[head] do
if not hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]] then
begin
hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]]:=true;
decs(i,j,now.a[i,j],d[head],-1);
decs(i,j,4,d[head],1);
simp(d[head]);
end else dec(head);
end;
end;
begin
assign(output,'garden.txt');rewrite(output);
fillchar(t,sizeof(t),$7f);
head:=1; tail:=0;
repeat
inc(tail);
expand(d[tail]);
until tail>head;
writeln('const a:array[1..340,1..5]of integer=(');
for i:=0 to 9 do
for j:=0 to 8 do
for k:=0 to 6 do
for l:=0 to 4 do
if t[i,j,k,l]<>2139062143 then
writeln('(',i,',',j,',',k,',',l,',',t[i,j,k,l],'),');
close(output);
end.
//2139062143
打表后的主程序:
const a:array[1..340,1..5]of integer=(
(1,0,0,0,1),
(1,1,0,0,2),
(1,1,1,0,4),
(1,1,2,0,5),
(1,2,0,0,3),
(1,2,1,0,5),
(1,2,1,1,6),
(1,2,1,2,9),
(1,2,2,0,6),
(1,2,2,1,7),
(1,2,2,2,9),
(1,2,2,4,13),
(1,2,3,0,7),
(1,2,3,1,9),
(1,2,3,2,10),
(1,2,3,3,13),
(1,2,4,0,9),
(1,2,4,1,10),
(1,2,4,2,12),
(1,2,5,0,10),
(1,2,5,1,12),
(1,2,6,0,12),
(1,3,0,0,4),
(1,3,0,1,9),
(1,3,1,0,6),
(1,3,1,1,7),
(1,3,1,2,10),
(1,3,1,3,13),
(1,3,1,4,15),
(1,3,2,0,7),
(1,3,2,1,8),
(1,3,2,2,10),
(1,3,2,3,12),
(1,3,3,0,8),
(1,3,3,1,10),
(1,3,3,2,11),
(1,3,4,0,10),
(1,3,4,1,11),
(1,3,5,0,11),
(1,4,0,0,5),
(1,4,0,1,10),
(1,4,0,2,13),
(1,4,1,0,7),
(1,4,1,1,8),
(1,4,1,2,11),
(1,4,1,3,13),
(1,4,2,0,8),
(1,4,2,1,10),
(1,4,2,2,11),
(1,4,3,0,10),
(1,4,3,1,11),
(1,4,4,0,11),
(1,5,0,0,7),
(1,5,0,1,11),
(1,5,0,2,14),
(1,5,0,3,16),
(1,5,1,0,8),
(1,5,1,1,10),
(1,5,1,2,12),
(1,5,2,0,10),
(1,5,2,1,11),
(1,5,3,0,11),
(1,6,0,0,8),
(1,6,0,1,12),
(1,6,0,2,14),
(1,6,1,0,10),
(1,6,1,1,11),
(1,6,2,0,11),
(1,7,0,0,10),
(1,7,0,1,13),
(1,7,1,0,11),
(1,8,0,0,11),
(2,0,0,0,2),
(2,0,1,0,5),
(2,0,2,0,6),
(2,1,0,0,3),
(2,1,1,0,4),
(2,1,1,1,6),
(2,1,1,2,8),
(2,1,2,0,5),
(2,1,2,1,7),
(2,1,2,2,9),
(2,1,2,4,13),
(2,1,3,0,7),
(2,1,3,1,9),
(2,1,3,2,10),
(2,1,3,3,13),
(2,1,4,0,8),
(2,1,4,1,10),
(2,1,4,2,13),
(2,1,5,0,10),
(2,1,5,1,13),
(2,1,6,0,13),
(2,2,0,0,4),
(2,2,0,1,7),
(2,2,0,2,10),
(2,2,1,0,5),
(2,2,1,1,6),
(2,2,1,2,8),
(2,2,1,3,11),
(2,2,1,4,12),
(2,2,2,0,6),
(2,2,2,1,8),
(2,2,2,2,10),
(2,2,2,3,12),
(2,2,3,0,8),
(2,2,3,1,9),
(2,2,3,2,11),
(2,2,4,0,9),
(2,2,4,1,11),
(2,2,5,0,10),
(2,3,0,0,5),
(2,3,0,1,7),
(2,3,0,2,10),
(2,3,0,3,14),
(2,3,0,4,16),
(2,3,1,0,6),
(2,3,1,1,7),
(2,3,1,2,9),
(2,3,1,3,11),
(2,3,2,0,7),
(2,3,2,1,9),
(2,3,2,2,11),
(2,3,3,0,9),
(2,3,3,1,10),
(2,3,4,0,10),
(2,4,0,0,6),
(2,4,0,1,8),
(2,4,0,2,11),
(2,4,0,3,13),
(2,4,1,0,7),
(2,4,1,1,9),
(2,4,1,2,11),
(2,4,2,0,9),
(2,4,2,1,10),
(2,4,3,0,10),
(2,5,0,0,7),
(2,5,0,1,10),
(2,5,0,2,12),
(2,5,1,0,9),
(2,5,1,1,10),
(2,5,2,0,10),
(2,6,0,0,9),
(2,6,0,1,12),
(2,6,1,0,10),
(2,7,0,0,10),
(3,0,0,0,3),
(3,0,1,0,5),
(3,0,1,1,7),
(3,0,1,2,9),
(3,0,2,0,6),
(3,0,2,1,8),
(3,0,2,2,10),
(3,0,2,4,14),
(3,0,3,0,8),
(3,0,3,1,10),
(3,0,3,2,11),
(3,0,3,3,14),
(3,0,4,0,9),
(3,0,4,1,11),
(3,0,4,2,14),
(3,0,5,0,11),
(3,0,5,1,14),
(3,0,6,0,14),
(3,1,0,0,4),
(3,1,0,1,7),
(3,1,0,2,9),
(3,1,1,0,5),
(3,1,1,1,7),
(3,1,1,2,9),
(3,1,1,3,11),
(3,1,1,4,13),
(3,1,2,0,6),
(3,1,2,1,8),
(3,1,2,2,10),
(3,1,2,3,12),
(3,1,3,0,7),
(3,1,3,1,9),
(3,1,3,2,11),
(3,1,4,0,9),
(3,1,4,1,11),
(3,1,5,0,11),
(3,2,0,0,5),
(3,2,0,1,7),
(3,2,0,2,9),
(3,2,0,3,12),
(3,2,0,4,13),
(3,2,1,0,6),
(3,2,1,1,7),
(3,2,1,2,9),
(3,2,1,3,11),
(3,2,2,0,7),
(3,2,2,1,8),
(3,2,2,2,10),
(3,2,3,0,8),
(3,2,3,1,10),
(3,2,4,0,9),
(3,3,0,0,6),
(3,3,0,1,8),
(3,3,0,2,10),
(3,3,0,3,12),
(3,3,1,0,7),
(3,3,1,1,8),
(3,3,1,2,9),
(3,3,2,0,8),
(3,3,2,1,9),
(3,3,3,0,9),
(3,4,0,0,7),
(3,4,0,1,9),
(3,4,0,2,10),
(3,4,1,0,8),
(3,4,1,1,9),
(3,4,2,0,9),
(3,5,0,0,8),
(3,5,0,1,10),
(3,5,1,0,9),
(3,6,0,0,9),
(4,0,0,0,4),
(4,0,0,1,8),
(4,0,0,2,10),
(4,0,1,0,6),
(4,0,1,1,8),
(4,0,1,2,10),
(4,0,1,3,12),
(4,0,1,4,14),
(4,0,2,0,7),
(4,0,2,1,9),
(4,0,2,2,11),
(4,0,2,3,13),
(4,0,3,0,8),
(4,0,3,1,10),
(4,0,3,2,12),
(4,0,4,0,10),
(4,0,4,1,12),
(4,0,5,0,12),
(4,1,0,0,5),
(4,1,0,1,8),
(4,1,0,2,10),
(4,1,0,3,12),
(4,1,0,4,14),
(4,1,1,0,6),
(4,1,1,1,8),
(4,1,1,2,10),
(4,1,1,3,12),
(4,1,2,0,7),
(4,1,2,1,9),
(4,1,2,2,11),
(4,1,3,0,8),
(4,1,3,1,10),
(4,1,4,0,10),
(4,2,0,0,6),
(4,2,0,1,8),
(4,2,0,2,10),
(4,2,0,3,12),
(4,2,1,0,7),
(4,2,1,1,8),
(4,2,1,2,10),
(4,2,2,0,8),
(4,2,2,1,9),
(4,2,3,0,9),
(4,3,0,0,7),
(4,3,0,1,9),
(4,3,0,2,10),
(4,3,1,0,8),
(4,3,1,1,9),
(4,3,2,0,9),
(4,4,0,0,8),
(4,4,0,1,10),
(4,4,1,0,9),
(4,5,0,0,9),
(5,0,0,0,5),
(5,0,0,1,9),
(5,0,0,2,11),
(5,0,0,3,13),
(5,0,0,4,15),
(5,0,1,0,7),
(5,0,1,1,9),
(5,0,1,2,11),
(5,0,1,3,13),
(5,0,2,0,8),
(5,0,2,1,10),
(5,0,2,2,12),
(5,0,3,0,9),
(5,0,3,1,11),
(5,0,4,0,11),
(5,1,0,0,6),
(5,1,0,1,9),
(5,1,0,2,11),
(5,1,0,3,13),
(5,1,1,0,7),
(5,1,1,1,9),
(5,1,1,2,11),
(5,1,2,0,8),
(5,1,2,1,10),
(5,1,3,0,9),
(5,2,0,0,7),
(5,2,0,1,9),
(5,2,0,2,11),
(5,2,1,0,8),
(5,2,1,1,9),
(5,2,2,0,9),
(5,3,0,0,8),
(5,3,0,1,10),
(5,3,1,0,9),
(5,4,0,0,9),
(6,0,0,0,6),
(6,0,0,1,10),
(6,0,0,2,12),
(6,0,0,3,14),
(6,0,1,0,8),
(6,0,1,1,10),
(6,0,1,2,12),
(6,0,2,0,9),
(6,0,2,1,11),
(6,0,3,0,10),
(6,1,0,0,7),
(6,1,0,1,10),
(6,1,0,2,12),
(6,1,1,0,8),
(6,1,1,1,10),
(6,1,2,0,9),
(6,2,0,0,8),
(6,2,0,1,10),
(6,2,1,0,9),
(6,3,0,0,9),
(7,0,0,0,7),
(7,0,0,1,11),
(7,0,0,2,13),
(7,0,1,0,9),
(7,0,1,1,11),
(7,0,2,0,10),
(7,1,0,0,8),
(7,1,0,1,11),
(7,1,1,0,9),
(7,2,0,0,9),
(8,0,0,0,8),
(8,0,0,1,12),
(8,0,1,0,10),
(8,1,0,0,9),
(9,0,0,0,9));
var
i,j,k,tot,ans,n:longint;
b:array[1..5]of longint;
begin
assign(input,'garden.in'); assign(output,'garden.out');
reset(input); rewrite(output);
readln(n);
for i:=1 to n do begin
ans:=maxlongint;
for j:=1 to 5 do read(b[j]); readln;
for k:=1 to 340 do begin
tot:=0;
for j:=1 to 4 do inc(tot,a[k][j]*b[j]);
if tot<b[5] then continue;
if a[k][5]<ans then ans:=a[k][5];
end;
if ans=maxlongint then writeln('Impossible')
else writeln(ans);
end;
close(input);close(output);
end.