Example #1
0
        static void Main(string[] args)
        {
            int input  = 1364;
            int width  = 50;
            int height = 50;

            Node[,] floorPlan = new Node[width, height];

            // Create the map as instructed.
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int sum   = x * x + 3 * x + 2 * x * y + y + y * y + input;
                    int count = 0;
                    while (sum != 0)
                    {
                        sum = sum & (sum - 1);
                        count++;
                    }
                    if (count % 2 == 0)
                    {
                        floorPlan[x, y] = new Node(new Point(x, y), true, -1, -1, NodeState.Untested, null);
                    }
                    else
                    {
                        floorPlan[x, y] = new Node(new Point(x, y), false, -1, -1, NodeState.Untested, null);
                    }
                }
            }

            // Search fastest way to target location (31,  39) from start location (1, 1).
            SearchParameters search = new SearchParameters(floorPlan[1, 1], floorPlan[31, 39], floorPlan, 50);
            List <Point>     path   = search.FindPath();

            Console.WriteLine("The length of the fastest route is " + path.Count);

            // Draw map of the fastest path.

            // Adds "Path" state to the points in the map where the path goes through.
            foreach (Point point in path)
            {
                floorPlan[point.x, point.y].State = NodeState.Path;
            }

            // Add 0-9 numbering on top of the map.
            int numbers = 0;

            Console.Write(' ');
            for (int i = 0; i < width; i++)
            {
                Console.Write(numbers);
                numbers++;
                if (numbers > 9)
                {
                    numbers = 0;
                }
            }
            Console.WriteLine();
            numbers = 0;

            // Draw map, line by line.
            for (int y = 0; y < height; y++)
            {
                // Start each line with 0-9 to signify which row is in question.
                Console.Write(numbers);
                floorPlan[1, 1].State = NodeState.Path;
                for (int x = 0; x < width; x++)
                {
                    if (floorPlan[x, y].State == NodeState.Path)
                    {
                        Console.Write('X');
                    }
                    else if (floorPlan[x, y].IsWalkable)
                    {
                        Console.Write('.');
                    }
                    else
                    {
                        Console.Write('#');
                    }
                }
                Console.WriteLine();
                numbers++;
                if (numbers > 9)
                {
                    numbers = 0;
                }
            }

            // Add numbers on bottom of the map.
            numbers = 0;
            Console.Write(' ');
            for (int i = 0; i < width; i++)
            {
                Console.Write(numbers);
                numbers++;
                if (numbers > 9)
                {
                    numbers = 0;
                }
            }
            Console.WriteLine();

            // Part 2: Do the breadth-first search.

            search.Reset();
            Console.WriteLine("The amount of distinct nodes reached: " + search.BreadthFirst());
        }