示例#1
0
        public static void Traverse(Dictionary<string, Node> graph, Node target, Node lookFor)
        {

            if (graph[target.Id].Visited)
            {
                return;
            }

            countOfMoves++;
            graph[target.Id].Visited = true;
            foreach (var item in graph[target.Id].Edges)
            {
                if (item.Destination.Id == lookFor.Id)
                {
                    result.Add(countOfMoves);
                    countOfMoves = 0;
                    return;
                }
            }
            
            foreach (var item in graph[target.Id].Edges)
            {
                Traverse(graph, item.Destination, lookFor);
            }

            
        }
示例#2
0
        //public static void Traverse(Dictionary<string, Node> graph, Node currNode, Node lookFor)
        //{

        //}

        static void Main(string[] args)
        {

            string targetName = Console.ReadLine();
            Node target = null;

            Dictionary<string, Node> graph = new Dictionary<string, Node>();

            int n = int.Parse(Console.ReadLine());

            string[] connection = null;

            Node firstConnection = null;
            Node secondConnetcion = null;

            for (int i = 0; i < n; i++)
            {
                connection = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                firstConnection = new Node(connection[0]);
                secondConnetcion = new Node(connection[1]);

                if (!graph.ContainsKey(connection[0]))
                {
                    if (connection[0] == targetName)
                    {
                        target = firstConnection;
                        start = target;
                    }
                    graph.Add(connection[0], firstConnection);
                }
                if (!graph.ContainsKey(connection[1]))
                {
                    graph.Add(connection[1], secondConnetcion);
                }
                graph[connection[0]].AddConection(new Edge(graph[connection[1]]));
                graph[connection[1]].AddConection(new Edge(graph[connection[0]]));

            }

            int m = int.Parse(Console.ReadLine());
            List<Node> lookFor = new List<Node>();


            Node currLookFor = null;
            string currLookForName = null;

            for (int i = 0; i < m; i++)
            {
               
                currLookForName = Console.ReadLine();
                currLookFor = new Node(currLookForName);
                ////////////////////////////////////
                Traverse(graph, target, currLookFor);
                ////////////////////////////////////
                lookFor.Add(currLookFor);

                notConnected = true;
                foreach (var item in graph)
                {
                    if (item.Value.Visited == false)
                    {
                        notConnected = false;
                    }
                    item.Value.Visited = false;
                }
                if (notConnected)
                {
                    result.Add(-1);
                    countOfMoves = 0;
                }
            }

            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
        }
示例#3
0
 public Edge(Node destination)
 {
     this.Destination = destination;
 }