I need my C program to be able to travel around a board. The board is represented by a 2D int array (where 0 and 7 are empty squares and everything else is an obstacle). The cost to move from one square to another is always the same but it should not move diagonally.
I’ve been looking up A* but it’s confusing and every single example I could find is with C++ or Java so I’m starting to wonder if it’s even possible on C.
That and if it’s the best algorithm to use for it.
Edit: The board is either 24×25 or 25×24 I can’t remember which
As your board is small, you can use a breadth-first search (BFS) doing a complete search for the best path. I think that the performance will be comparable if not better than A* algorithm.
The algorithm is simpler than A* and there are many implementations in C over the internet. Here is an example of a BFS transversal on a grid:
To get the path you can save a matrix (for example a 2d-array of char — lets name it
parent) whereparent[x][y]is for example (0 – if you reach that square from left, 1 – right, 2 – up, 3 – down). For example, if you’re visiting the square with coords (4,6) and will put the (5,6) on the queue, you doparent[5][6] = 2because (5,6) came from the row above (4,6). So to retrieve the full path you can pick the destination node and save theparentscoordinates until you reach the source square.Now it is up to you think about and figure how you can implement it 🙂