Friends who have played real-time strategies and RPG games will surely know that when we use the mouse to select certain units and command them to reach the location determined on the map, these units can always automatically select the shortest path to arrive. At this time, we will think of the well-known A * pathfinding algorithm. The following briefly introduces the algorithm implementation principle and attaches the C # implementation method.
The algorithm principle see: http://data.gameres.com/message.asp? TopicID = 25439
Code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Collections;
Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
Test mytest = new test ();
// Define the departure position
Point pa = new Point ();
Pa. x = 1;
Pa. y = 1;
// Define the destination
Point pb = new Point ();
Pb. x = 8;
Pb. y = 8;
Mytest. FindWay (pa, pb );
Mytest. PrintMap ();
Console. ReadLine ();
}
}
Class test
{
// The array uses 1 to indicate passing, and 0 to indicate obstacle.
Byte [,] R = new byte [10, 10] {
{1, 1, 1, 1, 1, 1, 1, 1, 1 },
{1, 1, 1, 1, 1, 1, 1, 1, 1 },
{1, 1, 1, 1, 0, 1, 1, 1, 1 },
{1, 1, 1, 1, 0, 1, 1, 1, 1 },
{1,