Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var tree      = new BinaryNode <char>('A');
            var leftNode  = new BinaryNode <char>('B');
            var rightNode = new BinaryNode <char>('S');

            leftNode.LeftNode            = new BinaryNode <char>('C');
            leftNode.RightNode           = new BinaryNode <char>('D');
            tree.LeftNode                = leftNode;
            rightNode.LeftNode           = new BinaryNode <char>('H');
            rightNode.LeftNode.LeftNode  = new BinaryNode <char>('G');
            rightNode.LeftNode.RightNode = new BinaryNode <char>('I');
            rightNode.RightNode          = new BinaryNode <char>('J');
            rightNode.RightNode.LeftNode = new BinaryNode <char>('M');
            tree.RightNode               = rightNode;

            var DFS = new DepthFirstSearch <char>();

            DFS.Explore(tree);

            var BFS = new BreadthFirstSearch <char>();

            BFS.Explore(tree);

            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void Main()
        {
            var start = new Point(9, 3);
            var end   = new Point(1, 8);
            Func <Point, char> getCell = GetCell(MAZE);
            Func <Point, IEnumerable <Point> > getNeighbours = GetNeighbours(getCell);
            Func <Point, Point, int>           getCost       = (from, to) => getCell(to) == '@' ? 4:1;
            Func <Point, int>    manhattanHeuristic          = (to) => Math.Abs(to.X - end.X) + Math.Abs(to.Y - end.Y);
            Func <Point, double> manhattanHeuristicD         = (to) => Math.Abs(to.X - end.X) + Math.Abs(to.Y - end.Y);

            var millisecondsTimeout = 100;

            var algorithms = new Dictionary <int, Tuple <string, Func <IEnumerable <Point> >, Func <IEnumerable <Point> > > >
            {
                { 1, "Depth First Search", () => DepthFirstSearch.Explore(start, getNeighbours), () => DepthFirstSearch.FindPath(start, getNeighbours, p => p.Equals(end)) },
                { 2, "Breadth First Search", () => BreadthFirstSearch.Explore(start, getNeighbours), () => BreadthFirstSearch.FindPath(start, getNeighbours, p => p.Equals(end)) },
                { 3, "Dijkstra", () => Dijkstra.Explore(start, getNeighbours, getCost), () => Dijkstra.FindPath(start, getNeighbours, getCost, p => p.Equals(end)) },
                { 4, "Greedy Best-First Search (with manhattan)", () => GreedyBestFirstSearch.Explore(start, getNeighbours, manhattanHeuristic), () => GreedyBestFirstSearch.FindPath(start, getNeighbours, manhattanHeuristic, p => p.Equals(end)) },
                { 5, "A* (with manhattan)", () => AStar.Explore(start, getNeighbours, getCost, manhattanHeuristicD), () => AStar.FindPath(start, getNeighbours, getCost, manhattanHeuristicD, p => p.Equals(end)) },
            };


            int choice = 0;

            while (choice != 10)
            {
                Console.Clear();
                Console.WriteLine("Choose traversal algorithm :");

                foreach (var algorithm in algorithms)
                {
                    Console.WriteLine($"\t{algorithm.Key} - {algorithm.Value.Item1}");
                }


                while (choice == 0)
                {
                    int.TryParse(Console.ReadLine(), out choice);
                }
                var currentMaze = MAZE;

                var result = algorithms[choice].Item2();
                var path   = algorithms[choice].Item3().ToList();

                var visited = new List <Point>();

                foreach (var item in result)
                {
                    visited.Add(item);
                    var lines = currentMaze.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                    var line  = lines[item.Y].ToCharArray();
                    line[item.X]  = '*';
                    lines[item.Y] = new string(line);
                    currentMaze   = string.Join(Environment.NewLine, lines);
                    DisplayMaze(currentMaze, visited.Count, 0, 0, algorithms[choice].Item1);

                    Thread.Sleep(millisecondsTimeout);
                    if (item == end)
                    {
                        break;
                    }
                }

                foreach (var item in path)
                {
                    var lines = currentMaze.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                    var line  = lines[item.Y].ToCharArray();
                    line[item.X]  = '+';
                    lines[item.Y] = new string(line);
                    currentMaze   = string.Join(Environment.NewLine, lines);
                }

                var pathcost = path.Aggregate(0, (current, point) => current + getCost(Point.Empty, point));

                DisplayMaze(currentMaze, visited.Count(), path.Count(), pathcost, algorithms[choice].Item1);

                choice = 0;
                Console.WriteLine();
                Console.WriteLine($"Press any key to reset");
                Console.Read();
            }
        }
Exemplo n.º 3
0
        public void Small_maze_test()
        {
            var start = new Point(9, 3);
            var end   = new Point(1, 5);

            var maze = @"
%%%%%%%%%%%%%%%%%%%%
%--------------%---%
%-%%-%%-%%-%%-%%-%-%
%--------P-------%-%
%%%%%%%%%%%%%%%%%%-%
%.-----------------%  
%%%%%%%%%%%%%%%%%%%%";

            Func <Point, char> getCell = GetCell(maze);
            Func <Point, IEnumerable <Point> > getNeighbours = GetNeighbours(getCell);


            var result = BreadthFirstSearch.Explore(start, getNeighbours);

            var path = new List <Point>();

            foreach (var item in result)
            {
                path.Add(item);
                if (item == end)
                {
                    break;
                }
            }

            var expected = new[] {
                new Point(9, 3),
                new Point(10, 3),
                new Point(10, 2),
                new Point(10, 1),
                new Point(11, 1),
                new Point(12, 1),
                new Point(13, 1),
                new Point(14, 1),
                new Point(13, 2),
                new Point(13, 3),
                new Point(14, 3),
                new Point(15, 3),
                new Point(16, 3),
                new Point(16, 2),
                new Point(16, 1),
                new Point(17, 1),
                new Point(18, 1),
                new Point(18, 2),
                new Point(18, 3),
                new Point(18, 4),
                new Point(18, 5),
                new Point(17, 5),
                new Point(16, 5),
                new Point(15, 5),
                new Point(14, 5),
                new Point(13, 5),
                new Point(12, 5),
                new Point(11, 5),
                new Point(10, 5),
                new Point(9, 5),
                new Point(8, 5),
                new Point(7, 5),
                new Point(6, 5),
                new Point(5, 5),
                new Point(4, 5),
                new Point(3, 5),
                new Point(2, 5),
                new Point(1, 5),
            };

            Assert.That(path, Is.EquivalentTo(expected));
        }