Answer the question
In order to leave comments, you need to log in
How to implement recursive wave algorithm function in c language?
There is a matrix, each element of which is registered as an element of the structure dynamically, with links to neighboring elements as pointers ways (0, 1, 2, 3) - these are directions - up, right, bottom, left, respectively.
typedef struct mapItems{
int id, xPos, yPos, distance, visited;
struct mapItem *ways[4];
}mapItem;
int wave(mapItem *current, int distance){
printf(" (%d:%d)b=%d-> ", current->xPos, current->yPos, back);
int i;
for(i=0; i<4; i++){
if((current->ways[i]!=0)&&!(current->ways[i]->visited)){
if(!current->ways[i]->visited){
current->ways[i]->distance=distance+1;
}
}
}
Answer the question
In order to leave comments, you need to log in
Somehow you overdid it, recursion is absolutely not needed here.
To begin with, we create a queue, include the starting point in it.
While the queue is not empty, we take a point from it, write down the length of the path to all its available empty neighbors and put them in the queue. You can interrupt the cycle even earlier, upon reaching the set point.
We restore the path in reverse.
I can't figure out how to recurse this function
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question