A * pathfinding algorithm is implemented based on C #

Source: Internet
Author: User

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,
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.