The first time I wrote a game with C. Writing algorithms on C # is indeed a challenge. If the time is too complex, the consequences will be much more obvious than C ++, so we always try to make things into O (N) or O (nlogn ). This time we implemented a path-finding algorithm.
The path search algorithm is like this: there is a square on 16 × 16.SomeThe end point. Things can only move up or down on the grid. Each grid needs to be recordedOne of the endpointsThe nearest pathAllDirection (just like the three-layer cyclic pathfinding algorithm, which finally gives the matrix ).
So I used the width-first search and a queue to solve this problem. At the beginning, it takes one second for an algorithm to write an error into an O (n2) result. After correction, the result is instantly displayed.
The algorithm is as follows:
1: add all the goal to the queue and mark it.
2: record the number of elements in the current queue.
3 ), mark this element (non-neighbor ).
4: if the number of pop-up elements is not as large as that recorded in step 1, go to step 3.
5: if the number of queues is not empty, go to 2.
6: end.
Run this algorithm to obtain the following layers:
This algorithm can be re-computed when an obstacle is added. You only need to mark the obstacle at the beginning.
C # is really the best choice for doing big jobs. It's really convenient to develop, and you don't have to worry about memory problems (becauseYesOccupies a lot of resources ).