type
data=record
x:longint;
y:longint;
z:longint;
end;
var
map:array[0..501,0..501]of longint;
m,n:longint;
statue:array[1..500]of data;
tab:array[1..501]of data;
tabcount:longint;
used:array[1..500]of boolean;
failr:boolean;
allstatue:array[1..500]of boolean;
max,bmax:longint;
count:longint;
var
i,j,k:longint;
procedure qsort(l,r:longint);
var mid,x,y:longint;
k:data;
begin
x:=l;
y:=r;
mid:=tab[(x+y)div 2].x;
repeat
while tab[x].x<mid do inc(x);
while tab[y].x>mid do dec(y);
if x<=y then
begin
k:=tab[x];
tab[x]:=tab[y];
tab[y]:=k;
inc(x);
dec(y);
end;
until x>y;
if y>l then qsort(l,y);
if x<r then qsort(x,r);
end;
procedure process(dn:longint);
var
sta:array[1..500,1..501]of boolean;
pro:array[1..250000]of data;
var
cou:longint;
i,j,k,l,m:longint;
qx,qy,qn:longint;
begin
cou:=1;
i:=1;
pro[1].x:=1;
pro[1].y:=dn;
sta[1,dn]:=true;
while i<=cou do
begin
qx:=pro[i].x;
qy:=pro[i].y;
qn:=map[qx,qy];
if (map[qx+1,qy]<qn) and (not(sta[qx+1,qy])) then
begin
inc(cou);
pro[cou].x:=qx+1;
pro[cou].y:=qy;
sta[qx+1,qy]:=true;
end;
if (map[qx-1,qy]<qn) and (not(sta[qx-1,qy])) then
begin
inc(cou);
pro[cou].x:=qx-1;
pro[cou].y:=qy;
sta[qx-1,qy]:=true;
end;
if (map[qx,qy+1]<qn) and (not(sta[qx,qy+1])) then
begin
inc(cou);
pro[cou].x:=qx;
pro[cou].y:=qy+1;
sta[qx,qy+1]:=true;
end;
if (map[qx,qy-1]<qn) and (not(sta[qx,qy-1])) then
begin
inc(cou);
pro[cou].x:=qx;
pro[cou].y:=qy-1;
sta[qx,qy-1]:=true;
end;
inc(i);
end;
i:=1;
while (sta[n,i]<>true)and(i<=m) do
begin inc(i);
end;
if i=m+1 then begin statue[dn].y:=0;statue[dn].x:=maxlongint; end
else statue[dn].x:=i;
while (sta[n,i]=true)and(i<=m) do inc(i);
if i=m+1 then begin statue[dn].y:=m end else
begin
j:=i;
k:=i;
while j<=m do
begin
if sta[n,j]=true then
for l:=k to j-1 do begin failr:=true;k:=j+1;end;
inc(j);
end;
if not failr then begin
statue[dn].y:=i-1;
end;
end;
for i:=1 to m do allstatue[i]:=allstatue[i] or sta[n,i];
end;
begin
assign(input,'flow.in');reset(input);
read(n,m);
for i:=1 to n do
for j:=1 to m do
read(map[i,j]);
for i:=0 to m+1 do
begin map[0,i]:=maxlongint;map[n+1,i]:=maxlongint; end;
for i:=0 to n+1 do
begin map[i,0]:=maxlongint;map[i,m+1]:=maxlongint; end;
i:=1;
while i<=m do
begin
if (map[1,i]>=map[1,i+1])or(i=m) then
begin
used[i]:=true;
process(i);
inc(i);
while map[1,i]<map[1,i-1] do inc(i);
end
else
inc(i);
end;
if failr then
begin
for i:=1 to m do if allstatue[i]=false then inc(max);
if max<>0 then
begin
writeln(0);
writeln(max);
end
end
else
begin
tabcount:=0;
for i:=1 to m do
if used[i] then
begin
inc(tabcount);
tab[tabcount]:=statue[i];
tab[tabcount].z:=i;
end;
qsort(1,tabcount);
max:=0;
for i:=1 to m do if allstatue[i]=false then inc(max);
if max<>0 then
begin
writeln(0);
writeln(max);
end
else
begin
i:=1;
max:=0;
inc(count);
while tab[i].x=1 do
begin
if tab[i].y>max then max:=tab[i].y;
inc(i);
end;
bmax:=max;
while (max<m) do
begin
while (tab[i].x<=bmax+1)and(i<>m+1) do
begin
if tab[i].y>max then max:=tab[i].y;
inc(i);
end;
inc(count);
bmax:=max;
end;
writeln(1);
writeln(count);
end;
end;
end.
为什么出现堆栈呢溢出呢??在自己电脑上用测试数据通过
const x:array[1..4] of longint=(0,0,1,-1);
y:array[1..4] of longint=(1,-1,0,0);
type b=record
l,r:integer;
end;
var map:array[1..501,1..501] of longint;
visit:array[1..501,1..501] of boolean;
a:array[1..501,1..501] of b;
f:array[1..501] of longint;
i,j,k,m,n,s,t,ans:longint;
procedure dfs(l,r:integer);
var i,j,u,v:integer;
begin
for i:=1 to 4 do
begin
u:=l+x[i]; v:=r+y[i];
if (u>0) and (u<=m) and (v>0) and (v<=n) and (map[l,r]>map[u,v]) then
begin
if not visit[u,v] then begin visit[u,v]:=true; dfs(u,v); end;
if a[u,v].l<a[l,r].l then a[l,r].l:=a[u,v].l;
if a[u,v].r>a[l,r].r then a[l,r].r:=a[u,v].r;
end;
end;
end;
begin
readln(m,n);
for i:=1 to m do
for j:=1 to n do read(map[i,j]);
fillchar(visit,sizeof(visit),false);
for i:=1 to m do
for j:=1 to n do
begin
a[i,j].l:=n+1; a[i,j].r:=0;
end;
for i:=1 to n do
begin
a[m,i].l:=i; a[m,i].r:=i;
end;
for i:=1 to n do
if not visit[1,i] then
begin
visit[1,i]:=true; dfs(1,i);
end;
s:=0;
for i:=1 to n do
if not visit[m,i] then inc(s);
if s>0 then begin writeln(0); writeln(s); halt; end;
for i:=1 to n do f[i]:=10000;
for i:=1 to n do
if a[1,i].l=1 then f[i]:=1;
for i:=2 to n do
for j:=1 to i-1 do
if ((a[1,j].r>=a[1,i].l) or (a[1,j].r+1=a[1,i].l)) and (f[i]>f[j]+1) then f[i]:=f[j]+1;
ans:=10000;
for i:=1 to n do
if (a[1,i].r=n) and (f[i]<ans) then ans:=f[i];
writeln(1); writeln(ans);
end.
不要把大数组放在函数里,函数里的数据是放在栈里的,栈的大小有限。
话说,NOIP给不给开编译开关来着?