Hi all,
i have written a very simple function to find a shortest path through a matrix, where each Node has a value, and the final return value is the minimal cost to cross from starting point to end point while adding all values on the way.
It works fine for matrices up to 6*6, but when I try bigger ones, it hangs.
I'm not very fluent with algorithms, can someone help me find a more efficient algorithm?
on the code side:
All .status are started with "=CLOSED".
"LARGE" is defined to be "MAX_LONG"
i have written a very simple function to find a shortest path through a matrix, where each Node has a value, and the final return value is the minimal cost to cross from starting point to end point while adding all values on the way.
It works fine for matrices up to 6*6, but when I try bigger ones, it hangs.
I'm not very fluent with algorithms, can someone help me find a more efficient algorithm?
on the code side:
All .status are started with "=CLOSED".
"LARGE" is defined to be "MAX_LONG"
Rich (BB code):
long int GetMin(Node** matrix,int currentR,int currentC,int endR,int endC,
int rows,int columns)
{
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;
//if not reached end
matrix[currentR][currentC].status=OPEN;
result=Smallest4(
GetMin(matrix,currentR,currentC+1,endR,endC,rows,columns),
GetMin(matrix,currentR,currentC-1,endR,endC,rows,columns),
GetMin(matrix,currentR+1,currentC,endR,endC,rows,columns),
GetMin(matrix,currentR-1,currentC,endR,endC,rows,columns));
//set current status to closed- 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;
}