예제 #1
0
        public int HowManyGenderInNetwork(int level, Panda panda, GenderType gender)
        {
            List <Panda>           visited = new List <Panda>();
            Queue <PandaWithLevel> queue   = new Queue <PandaWithLevel>();

            int counter = 0;

            visited.Add(panda);
            queue.Enqueue(new PandaWithLevel()
            {
                Panda = panda,
                Level = 0
            });

            while (queue.Count > 0)
            {
                PandaWithLevel temp = queue.Dequeue();

                if ((temp.Level == level) && (temp.Panda.Gender == gender))
                {
                    counter++;
                }

                foreach (Panda p in FriendsOf(temp.Panda))
                {
                    if (!visited.Contains(p))
                    {
                        visited.Add(p);
                        queue.Enqueue(new PandaWithLevel()
                        {
                            Panda = p,
                            Level = temp.Level + 1
                        });
                    }
                }
            }

            return(counter);
        }
예제 #2
0
        private int FindConnectionLevel(Panda startPanda, Panda endPanda)
        {
            List <Panda>           visited = new List <Panda>();
            Queue <PandaWithLevel> queue   = new Queue <PandaWithLevel>();

            visited.Add(startPanda);
            queue.Enqueue(new PandaWithLevel()
            {
                Panda = startPanda,
                Level = 0
            });

            while (queue.Count > 0)
            {
                PandaWithLevel temp = queue.Dequeue();

                if (temp.Panda.Equals(endPanda))
                {
                    return(temp.Level);
                }

                foreach (Panda panda in FriendsOf(temp.Panda))
                {
                    if (!visited.Contains(panda))
                    {
                        visited.Add(panda);
                        queue.Enqueue(new PandaWithLevel()
                        {
                            Panda = panda,
                            Level = temp.Level + 1
                        });
                    }
                }
            }

            return(-1);
        }