Hi,

This function is a solution for recursivly finding the lowest cost path between two points in a 2D matrix. the cost of each step is its ".value". it's passed with the cordinates for the start and end points.

However, somethings not working here and I get the wrong results.

i think this is similar to the A* pathfinder but the only examples i have seen are not recurisive...

LARGE is the limit of "long int"

Code:

#include "targ62.h"
long int GetMin(Node** matrix,int currentR,int currentC,int endR,int endC,
int rows,int columns,Node* father)
{
long int result;
//if out of bounds return max of field
if (currentR==rows || currentC==columns || currentR<0 || currentC<0)
return LARGE;
//if reached end
if (currentC==endC && currentR==endR)
return (matrix[currentR][currentC].value);
//if already checked here
if (matrix[currentR][currentC].status==OPEN)
return LARGE;
/*record distance from beggining*/
//if first node
if (father==NULL)
//assign first value as best value
matrix[currentR][currentC].bestFromStart=
matrix[currentR][currentC].value;
//if not first node
else
{
//if bestFromStart old value higher than current value
if ( (matrix[currentR][currentC].value +
father->bestFromStart ) <
matrix[currentR][currentC].bestFromStart )
//then...
matrix[currentR][currentC].bestFromStart=
matrix[currentR][currentC].value+
father->bestFromStart;
else
/*this is a bad path -
this way is not shorter than previously found way*/
return LARGE;
}
//if not reached end
matrix[currentR][currentC].status=OPEN;
result=Smallest4(
GetMin(matrix,currentR,currentC+1,endR,endC,rows,columns,
matrix[currentR,currentC]),
GetMin(matrix,currentR,currentC-1,endR,endC,rows,columns,
matrix[currentR,currentC]),
GetMin(matrix,currentR+1,currentC,endR,endC,rows,columns,
matrix[currentR,currentC]),
GetMin(matrix,currentR-1,currentC,endR,endC,rows,columns,
matrix[currentR,currentC]));
//set current status to closes- can try here again
matrix[currentR][currentC].status=CLOSED;
//if was blocked all directions
if (result==LARGE)
return LARGE;
return matrix[currentR][currentC].value+result;
}

the function is called with the following starting parms:

Code:

result=GetMin(matrix,startR,startC,endR,endC,rows,columns,NULL);