Please help!!! Why my code get Crash (ACCESS_VIOLATION) #4?
Послано
REM 8 июн 2005 20:07
Program Acm_Timus_Ru_1298;
const dy:array [1..8] of integer=(-2,-2,-1,1,2,2,1,-1);
dx:array [1..8] of integer=(-1,1,2,2,1,-1,-2,-2);
g:array [1..8] of char=('a','b','c','d','e','f','g','h');
var a:array [-5..15,-5..15] of longint;
k,i,j,ip,min,h,x,y,rem:longint;
n : longint;
p : boolean;
Function mo(x,y:integer):boolean;
Begin
if (x>n) or (x<1) or (y>n) or (y<1) then mo:=false
else mo:=a[x,y]=0;
End;
Function hm(x,y:integer):byte;
Var i:integer;
Begin
k:=0;
if not(mo(x,y)) then k:=n*n
else for i:=1 to 8 do
if mo(x+dx[i],y+dy[i]) then k:=k+1;
hm:=k;
End;
BEGIN
readln(n);
if (n>=1) and (n<=8) then
begin
fillchar(a,sizeof(a),0);
x:=1;y:=1;
for i:=1 to n*n do
begin
min:=n*n;
a[x,y]:=i;
for j:=1 to 8 do
begin
h:=hm(x+dx[j],y+dy[j]);
if h<min then
begin
ip:=j;
min:=h;
end;
end;
x:=x+dx[ip];
y:=y+dy[ip];
end;
p:=true;
for i:=1 to n do
for j:=1 to n do
if a[i,j]=0 then p:=false;
if p=false then begin writeln('IMPOSSIBLE');end
else
for k:=1 to n*n do
for i:=1 to n do
for j:=1 to n do
if a[i,j]=k then writeln(g[i],j);
end
else
writeln('IMPOSSIBLE');
END.
Re: Please help!!! Why my code get Crash (ACCESS_VIOLATION) #4?
Послано
Nirjon 8 июн 2005 23:14
I think the problem is here-
x:=x+dx[ip];
y:=y+dy[ip];
may be ip is invalid since it might not be updated. When all the neighbor squares return hm()>=min value, ip will not get updated.
Re: Please help!!! Why my code get Crash (ACCESS_VIOLATION) #4?
It's very intresting!
I compiled given source code both on
Delphi and FreePascal and checked each
program on every possible tests (n from 1 to 8)
and I didn't get Access Violation.
I am really confused if this code get Access Violation
on Timus Judge System.
Re: Please help!!! Why my code get Crash (ACCESS_VIOLATION) #4?
Послано
REM 9 июн 2005 23:05
It's very interesting, I wrote a new program in which answers for 5,6,7,8 were constants (which I defined by my program which gets Crash on a server), and I got AC.
Re: For n=2 the 'ip' variable is not initialized!
I know that ip was not initialized.
Delphi gave that warning, but why
does the same program run different on
my home machine and on timus server.
I have no crashes on all possible tests.
How do you compile sources exactly?