讨论 / 矩阵取数游戏 为什么爆堆栈?
高原碧水 2010-08-26 10:10:00
点我顶贴 收藏 删除
这是我的程序

type arr=array[1..50] of longint;

var n,m,i,j,wei:longint;

a:array[1..80] of longint;

first: integer; last: integer;

zo:array[1..50] of longint;

f:array[1..80,1..80] of arr;

bo:array[1..80,1..80] of boolean;

l:array[1..80] of arr;

boo:array[1..80] of boolean;

function max(u1,u2:arr):arr;

var i:integer; lo,lp:integer;

begin

for i:=50 downto 1 do

begin

if u1[i]=-1 then lo:=i-1;

if u2[i]=-1 then lp:=i-1;

end;

if lo>lp then exit(u1); if lp>lo then exit(u2);

for i:=lo downto 1 do

begin

if u1[i]>u2[i] then exit(u1);

if u2[i]>u1[i] then exit(u2);

end;

exit(u1);

end;

function jiafa(x,y:arr):arr;

var i,j:integer; dai:arr;

begin

fillchar(dai,sizeof(dai),0); fillchar(jiafa,sizeof(jiafa),0);

i:=1; j:=0;

while (x[i]<>-1) or (y[i]<>-1) do

begin

if x[i]=-1 then begin x[i]:=0; x[i+1]:=-1; end;

if y[i]=-1 then begin y[i]:=0; y[i+1]:=-1; end;

wei:=i;

dai[i]:=x[i]+y[i]+j;

j:=dai[i] div 10;

dai[i]:=dai[i] mod 10;

inc(i);

end;

if j>0 then begin dai[i]:=j; wei:=i; end;

while (dai[i]=0) and (i<>1) do

dec(i);

dai[i+1]:=-1;

exit(dai);

end;

function chengfa(k:arr; h:longint):arr;

var i,t,j:integer; dai:arr;

begin

fillchar(dai,sizeof(dai),0);

fillchar(chengfa,sizeof(chengfa),0);

i:=1; t:=0;

while k[i]<>-1 do

begin

j:=i;

dai[i]:=k[i]*h+dai[i];

t:=dai[i] div 10;

dai[i]:=dai[i] mod 10;

while t>0 do

begin

inc(j);

dai[j]:=dai[j]+t;

t:=dai[j] div 10;

dai[j]:=dai[j] mod 10;

end;

inc(i);

end;

while (dai[j]=0) and (j<>1) do

dec(j);

dai[j+1]:=-1;

exit(dai);

end;

function dfs(b,c,x:integer):arr;

var i,j:integer;

begin

fillchar(dfs,sizeof(dfs),0);

if bo[b,c] then exit(f[b,c]);

if not boo[x] then

begin

l[x]:=chengfa(l[x-1],2);

boo[x]:=true;

end;

if c=b then begin exit(chengfa(l[x],a[b])); end;

dfs:=max(jiafa(chengfa(l[x],a[b]),dfs(b+1,c,x+1)),jiafa(chengfa(l[x],a[c]),dfs(b,c-1,x+1)));

f[b,c]:=dfs;

bo[b,c]:=true;

end;

begin

readln(n,m); fillchar(zo,sizeof(zo),0); zo[2]:=-1; l[0][1]:=1; l[0][2]:=-1;

for i:=1 to n do

begin

{ fillchar(f,sizeof(f),0); fillchar(bo,sizeof(bo),false);}

first:=1; last:=m;

for j:=1 to m do

read(a[j]);

zo:=jiafa(zo,dfs(first,last,1));

end;

for i:=wei downto 1 do

write(zo[i]);

writeln;

end.

假如没有括号的话程序是对的,但提交上去连第一个测试点都爆堆栈(在一个测试系统上是错误号:202,而在rqnoj上是保护性错误),把两句fillchar套上大括号,就没有什么爆堆栈了,虽然程序只得了10分.

请问为什么去掉就没有爆堆栈了?

乞请指教.

查看更多回复
提交回复