Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Array speed

reality29reality29 Posts: 3Member
Hi all! I have to do a "chess knight's tour". The tour is working great but it's getting slow when the board is bigger than 6x6. I found out (using GetTickCount) that it's slow because of the arrays. Is there a way to make it faster?
[code]program Main;

uses boardunit, windows;

function searchForPath(var board:Board; x, y:Integer):Boolean;
var currentMove, _x, _y:Integer;
boundaryError, pathFound, testError:Boolean;
t1, t2:Longint;
begin
boundaryError:=false;
pathFound:=false;
currentMove:=0;
while(currentMove<8)do
begin
//t1:=GetTickCount;
_x:=x + board._x[currentMove];
_y:=y + board._y[currentMove];
//t2:=GetTickCount;
//if((t2 - t1)>0)then Writeln('diff:', t2 - t1);
addStep(board, _x, _y, board.currentStep, boundaryError);
if(boundaryError=false)then
begin
board.currentStep:=board.currentStep + 1;
if(board.currentStep>board.boardSize)then
begin
searchForPath:=true;
exit;
end
else
begin
pathFound:=searchForPath(board, _x, _y);
if(pathFound=true)then
begin
searchForPath:=true;
exit;
end
else
begin
addStep(board, _x, _y, 0, boundaryError);
board.currentStep:=board.currentStep - 1;
end;
end;
end;
currentMove:=currentMove + 1;
end;
searchForPath:=false;
end;
var n, x, y:Integer;
test :Board;
err1, pathFound :Boolean;
mem:Longint;
failas:String;
begin
WriteLn('Output file name:');
readln(failas);
Assign(test.fl, failas);
Rewrite(test.fl);
WriteLn('Board size:');
ReadLn(n);
if(init(test, n))then
begin
WriteLn('Board created');
WriteLn('Where is the knight standing?:');
ReadLn(x, y);
err1:=false;
addStep(test, x, y, test.currentStep, err1);
while(err1)do
begin
WriteLn('Wrong coordinates! Where is the knight standing?:');
ReadLn(x, y);
addStep(test, x, y, 1, err1);
end;
test.currentStep:=test.currentStep+1;
Writeln('Searching for path...');
pathFound:=searchForPath(test, x, y);
if(pathFound)then
begin
writeln(test.fl, 'Path found when N = ', n, ', x = ', x, ', y = ', y, ':');
writeln(test.fl);
Writeln;
Writeln('Path found!');
printBoard(test, err1);
WriteLn('Path printed to ', failas, ' file.');
end
else
begin
writeln(test.fl, 'Path not found when N = ', n, ', x = ', x, ', y = ', y, ':');
writeln(test.fl);
WriteLn('Path not found');
end;
Close(test.fl);
end
else WriteLn('Error while creating the board!');
readln;
end.[/code]

[code]Unit boardunit;

interface
uses windows;
type Board = record
boardSize, boardSizeSqrt, currentStep:Integer;
initialized:Boolean;
_x:Array [0..7] of Integer;
_y:Array [0..7] of Integer;
arr:Array [1..100, 1..100] of Integer;
fl: Text;
end;

// *---------------------------------------------------------------------------------------------------------------------------------------------------* \
Function init(var myBoard:Board; n:Integer):Boolean;
Function exists(var myBoard:Board):Boolean;
Procedure addStep(var myBoard:Board; x, y, step:Integer; var boundaryError:Boolean);
Procedure printBoard(var myBoard:Board; var error1:Boolean);
// *---------------------------------------------------------------------------------------------------------------------------------------------------* \

implementation
Function init(var myBoard:Board; n:Integer):Boolean;
var i, j:Integer;
err1, err2:Boolean;
begin
if(myBoard.initialized = false)and(n<=100)then
begin
myBoard.initialized:=true;
myBoard.boardSize:=n*n;
myBoard.boardSizeSqrt:=n;
myBoard.currentStep:=1;
myBoard._x[0]:=2;
myBoard._x[1]:=1;
myBoard._x[2]:=-1;
myBoard._x[3]:=-2;
myBoard._x[4]:=-2;
myBoard._x[5]:=-1;
myBoard._x[6]:=-1;
myBoard._x[7]:=2;
myBoard._y[0]:=1;
myBoard._y[1]:=2;
myBoard._y[2]:=2;
myBoard._y[3]:=1;
myBoard._y[4]:=-1;
myBoard._y[5]:=-2;
myBoard._y[6]:=-2;
myBoard._y[7]:=-1;
i:=1;
j:=1;
while(i<=n)do
begin
j:=1;
while(j<=n)do
begin
myBoard.arr[j, i]:=0;
j:=j+1;
end;
i:=i+1;
end;
init:=true;
end
else init:=false;
end;
Function exists(var myBoard:Board):Boolean;
begin
if(myBoard.initialized=true)then exists:=true
else exists:=false;
end;
Procedure addStep(var myBoard:Board; x, y, step:Integer; var boundaryError:Boolean);
var i, j, su:Integer;
errTest :Boolean;
//t1, t2:Longint;
begin

if(myBoard.initialized=true)then
begin
if(x<1)or(x>myBoard.boardSizeSqrt)or(y<1)or(y>myBoard.boardSizeSqrt)then boundaryError:=true
else
begin
//t1:=GetTickCount;
if(myBoard.arr[y, x]<>0)and(step<>0)then boundaryError:=true
else
begin
myBoard.arr[y, x]:=step;
boundaryError:=false;
end;
//t2:=GetTickCount;
//if((t2 - t1)>0)then Writeln('diff:', t2 - t1);
end;
end;

end;
Procedure printBoard(var myBoard:Board; var error1:Boolean);
var i, x, j:Integer;
begin
if(myBoard.initialized=false)then error1:=true
else
begin
error1:=false;
i:=1;
while(i<=myBoard.boardSizeSqrt)do
begin
j:=1;
while(j<=myBoard.boardSizeSqrt)do
begin
write(myBoard.arr[j, i]:3);
write(myBoard.fl, myBoard.arr[j, i]:3);
j:=j+1;
end;
writeln;
writeln;
writeln(myBoard.fl);
writeln(myBoard.fl);
i:=i+1;
end;
end;
end;
end.[/code]
Sign In or Register to comment.