Общий форумHow can this program get Memory Limit Exceeded???(Problem 1145 - Labyrint) 2 arrays 300 by 1000 give us 600 Kb, not 1000+! Usually pascal compiler adds 260 kb, but not more than 300! And when I use only one array a, I get time limit, but with memory usage of 800K instead of 500! How is this possible? Help me, please! Program Labyrint; Const Max=1000; Var a,b:Array[-1..300,-1..Max+1] Of Byte; n,m,i,j,x,y,mx,my:Integer; m2:Array[0..3] Of Byte; Procedure PutV(X,Y:Integer; V:Byte); Var dx:Integer; Begin dx:=m2[X Mod 4]; X:=X Shr 2; a[X,Y]:=a[X,Y] And (255 Xor (3 Shl dx)); a[X,Y]:=a[X,Y] Or (V Shl dx); End; Function GetV(X,Y:Integer):Byte; Var dx,v:Integer; Begin dx:=m2[X Mod 4]; X:=X Shr 2; GetV:=(a[X,Y] And (3 Shl dx)) Shr dx; End; Procedure ReadData; Var i,j:Integer; s:String; Procedure Check(i,j:Integer); Begin If GetV(i,j)=1 Then If GetV(i+1,j)+GetV(i-1,j)+GetV(i,j+1)+GetV(i,j-1)<=1 Then Begin x:=i; y:=j; End; End; Begin Readln(n,m); FillChar(a,SizeOf(a),0); x:=0; y:=0; For i:=1 To m Do Begin Readln(s); For j:=1 To n Do Begin If s[j]='.' Then PutV(j,i,1); If x=0 Then Check(j-1,i-1); End; End; For i:=1 To m Do Check(n,i); For i:=1 To n Do Check(i,m); End; Procedure FindLone; Var i,j:Integer; Begin x:=0; y:=0; For i:=1 To n Do For j:=1 To m Do If GetV(i,j)=1 Then If GetV(i+1,j)+GetV(i-1,j)+GetV(i,j+1)+GetV(i,j-1)<=1 Then Begin x:=i; y:=j; Exit; End; End; Procedure ShowField; Var i,j:Integer; Begin Writeln; For i:=1 To m Do Begin For j:=1 To n Do Write(GetV(j,i)); Writeln; End; End; Function FindMaxXY(x,y:Integer; Var mx,my:Integer):Integer; Var i,j,l,ml,n2:Integer; Function Check(dx,dy,v:Integer):Boolean; Begin If GetV(i+dx,j+dy)=v Then Begin i:=i+dx; j:=j+dy; If v=1 Then Inc(l) Else Dec(l); Check:=True End Else Check:=False; End; Begin i:=x; j:=y; l:=0; ml:=-1; Repeat n2:=0; { ShowField; Readln;} If GetV(i,j)=1 Then PutV(i,j,2) Else Begin If GetV(i+1,j)=2 Then Inc(n2); If GetV(i-1,j)=2 Then Inc(n2); If GetV(i,j+1)=2 Then Inc(n2); If GetV(i,j-1)=2 Then Inc(n2); If n2<1 Then PutV(i,j,GetV(i,j)+1); End; If Not Check(1,0,1) Then If Not Check(-1,0,1) Then If Not Check(0,1,1) Then If Not Check(0,-1,1) Then Begin If l>ml Then Begin mx:=i; my:=j; ml:=l; End; If GetV(i,j)=2 Then PutV(i,j,3); If Not Check(1,0,2) Then If Not Check(-1,0,2) Then If Not Check(0,1,2) Then Check(0,-1,2) End; Until ((i=x) And (j=y)); FindMaxXY:=ml; End; Begin For i:=0 To 3 Do m2[i]:=2*i; FillChar(a,SizeOf(a),0); ReadData; Move(a,b,SizeOf(b)); If x=0 Then Begin Writeln(0); Halt; End; FindMaxXY(x,y,mx,my); For i:=1 To n Do For j:=1 To m Do If GetV(i,j)>0 Then PutV(i,j,1); Writeln(FindMaxXY(mx,my,x,y)); End. Now I got AC by changing my code a bit, but the program ran in 0.98 sec and used 835K with only one array! Could anyone tell me how to improve my algorythm ?(+) My algorythm is as follows : 1. Choose any free cell c1 with only 1 or 0 neighbours - O(n*m) 2. Find the cell c2 for wich length of path to c1 is maximum - O(n*m) 3. Find the cell c3 for wich length of path to c2 is maximum - O(n*m) Then the length of path from c2 to c3 is the answer. I use width search for steps 2 and 3, so they should run in O(n*m). And, I only use one array [-1..250,-1..1010] Of Byte! Why does this program run in 0.98 sec and 885 Kb??? If anyone could help me, email me please and I will send you my code (I don't leave it here because Marat Bakirov won't be happy to see an accepted program on the webboard:) Re: I know why you get Memory Limit ! You use string. Delphi compiles it to 4000000000 byte. So you must use an array of char. But when you get WA :=)). I don't know why. You can try to do it. My e-ma > My algorythm is as follows : > 1. Choose any free cell c1 with only 1 or 0 neighbours - O(n*m) > 2. Find the cell c2 for wich length of path to c1 is maximum - O (n*m) > 3. Find the cell c3 for wich length of path to c2 is maximum - O (n*m) > Then the length of path from c2 to c3 is the answer. > I use width search for steps 2 and 3, so they should run in O(n*m). > And, I only use one array [-1..250,-1..1010] Of Byte! Why does this > program run in 0.98 sec and 885 Kb??? > If anyone could help me, email me please and I will send you my code > (I don't leave it here because Marat Bakirov won't be happy to see an > accepted program on the webboard:) Re: il: nsc@ufacom.ru > > My algorythm is as follows : > > 1. Choose any free cell c1 with only 1 or 0 neighbours - O(n*m) > > 2. Find the cell c2 for wich length of path to c1 is maximum - O > (n*m) > > 3. Find the cell c3 for wich length of path to c2 is maximum - O > (n*m) > > Then the length of path from c2 to c3 is the answer. > > I use width search for steps 2 and 3, so they should run in O (n*m). > > And, I only use one array [-1..250,-1..1010] Of Byte! Why does this > > program run in 0.98 sec and 885 Kb??? > > If anyone could help me, email me please and I will send you my > code > > (I don't leave it here because Marat Bakirov won't be happy to see > an > > accepted program on the webboard:) |