예제 #1
0
        static bool CheckFriendConnectionDFSRecursive(Panda current, string target, HashSet <Panda> visited)
        {
            Console.WriteLine("Currently at " + current.Name);
            visited.Add(current);

            foreach (Panda friend in current.Friends)
            {
                if (!visited.Contains(friend))
                {
                    if (friend.Name == target)
                    {
                        return(true);
                    }

                    Console.WriteLine("Checking friend " + friend.Name);

                    visited.Add(friend);
                    if (CheckFriendConnectionDFSRecursive(friend, target, visited))
                    {
                        return(true);
                    }

                    Console.WriteLine("Finished checking friend " + friend.Name);
                }
            }

            return(false);
        }
예제 #2
0
        static bool CheckFriendConnectionBFS(string name1, string name2, SocialNetwork sn)
        {
            Queue <Panda>   q       = new Queue <Panda>();
            HashSet <Panda> visited = new HashSet <Panda>();

            q.Enqueue(sn[name1]);
            visited.Add(sn[name1]);

            Console.WriteLine("Starting from " + name1);
            while (q.Count > 0)
            {
                Panda current = q.Dequeue();
                Console.WriteLine("Currently at " + current.Name);
                foreach (Panda friend in current.Friends)
                {
                    if (friend.Name == name2)
                    {
                        return(true);
                    }

                    if (!visited.Contains(friend))
                    {
                        Console.WriteLine("Added friend " + friend.Name);
                        q.Enqueue(friend);
                        visited.Add(friend);
                    }
                }
            }

            return(false);
        }
예제 #3
0
        static void Main(string[] args)
        {
            //STATIC EXAPLE
            Panda p1 = new Panda("Panda1");
            Panda p2 = new Panda("Panda2");

            p1.Name = "Nikola"; //promenice samo ime vec postojece p1 pande

            Console.WriteLine("panda1 name:" + p1.Name);
            Console.WriteLine("panda2 name: " + p2.Name);

            Console.WriteLine("panda population: " + Panda.Population);
            Console.WriteLine();

            //VALUE AND REFERENCE TYPES
            //value types
            Point po1 = new Point();

            po1.x = 7;
            Point po2 = po1;

            Console.WriteLine("po1.x: " + po1.x);
            Console.WriteLine("po2.x" + po2.x);

            po1.x = 9;

            Console.WriteLine("po1.x: " + po1.x);
            Console.WriteLine("po2.x" + po2.x);
            Console.WriteLine();

            //reference types

            PointRef po1Ref = new PointRef();

            po1Ref.x = 7;

            PointRef po2Ref = po1Ref;

            Console.WriteLine("po1Ref.x: " + po1Ref.x);
            Console.WriteLine("po2Ref.x: " + po2Ref.x);

            po1Ref.x = 9;

            Console.WriteLine("po1Ref.x: " + po1Ref.x);
            Console.WriteLine("po2Ref.x: " + po2Ref.x);


            Console.ReadLine();
        }