Beispiel #1
0
        public static void SetUpUnDirectedGraph(KarthicGraph <int> graph)
        {
            GraphNode <int> node1 = new GraphNode <int>(0);
            GraphNode <int> node2 = new GraphNode <int>(1);
            GraphNode <int> node3 = new GraphNode <int>(2);
            GraphNode <int> node4 = new GraphNode <int>(3);
            GraphNode <int> node5 = new GraphNode <int>(4);
            GraphNode <int> node6 = new GraphNode <int>(5);
            GraphNode <int> node7 = new GraphNode <int>(6);

            //Add vertices
            graph.AddNode(node1);
            graph.AddNode(node2);
            graph.AddNode(node3);
            graph.AddNode(node4);
            graph.AddNode(node5);
            graph.AddNode(node6);
            graph.AddNode(node7);

            //Add edges

            graph.AddUndirectedEdge(node1, node2);
            graph.AddUndirectedEdge(node1, node3);
            graph.AddUndirectedEdge(node1, node4);

            graph.AddUndirectedEdge(node2, node5);
            graph.AddUndirectedEdge(node5, node7);

            graph.AddUndirectedEdge(node4, node6);
            //graph.AddDirectedEdge(new GraphNode<int>(4), new GraphNode<int>(6));
        }
        public bool BuildGraph(int startx, int starty, int size, char endgoal, char[,] maze, GraphNode <char> parent, KarthicGraph <char> graph)
        {
            //Base case
            //check for outside bounds
            if (startx < 0 || startx >= size || starty < 0 || starty >= size)
            {
                return(false);
            }

            //check for obstacle
            if (maze[startx, starty] == '#')
            {
                return(false);
            }
            //check if already visited..to prevent loop
            if (maze[startx, starty] == '+')
            {
                return(false);
            }


            //when the code comes means the path is free and goal not found so traverse in all direction
            GraphNode <char> child = new GraphNode <char>(maze[startx, starty]);

            child.XAxis = startx;
            child.YAxis = starty;
            // child.Marked = true;
            graph.AddNode(child);
            graph.AddDirectedEdge(parent, child);
            //parent.Neighbors.Add(child);


            //check for goal
            if (maze[startx, starty] == endgoal)
            {
                return(true);
            }

            //Mark the current point as visited
            maze[startx, starty] = '+';


            //search in north of the point
            if (BuildGraph(startx - 1, starty, size, endgoal, maze, child, graph))
            {
                return(true);
            }

            //search in the east of the point
            if (BuildGraph(startx, starty + 1, size, endgoal, maze, child, graph))
            {
                return(true);
            }

            //search in the south of the point
            if (BuildGraph(startx + 1, starty, size, endgoal, maze, child, graph))
            {
                return(true);
            }

            //search in the west of the point
            if (BuildGraph(startx, starty - 1, size, endgoal, maze, child, graph))
            {
                return(true);
            }

            //goal not found ..unmark the point
            maze[startx, starty] = '.';
            // child.Marked = false;

            return(false);
        }