예제 #1
0
파일: Program.cs 프로젝트: mckayfenn/CS4150
        public static Star findMajority(List <Star> A, long d)
        {
            if (A.Count == 0)
            {
                return(null);
            }
            else if (A.Count == 1)
            {
                return(A.ElementAt(0));
            }
            else
            {
                // Split into halves and recursively solve them
                List <Star> firstHalf = new List <Star>();
                for (int i = 0; i < A.Count / 2; i++)
                {
                    firstHalf.Add(A.ElementAt(i));
                }
                List <Star> secondHalf = new List <Star>();
                for (int i = A.Count / 2; i < A.Count; i++)
                {
                    secondHalf.Add(A.ElementAt(i));
                }
                Star x = findMajority(firstHalf, d);
                Star y = findMajority(secondHalf, d);

                if (x == null && y == null)
                {
                    return(null);
                }



                else if (x == null)
                {
                    // count occurences of y in A, return y or NO
                    int count = 0;
                    foreach (Star s2 in A)
                    {
                        if (distanceBetweenStars(y, s2, d))
                        {
                            count++;
                        }
                    }

                    if (count > A.Count / 2)
                    {
                        return(y);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else if (y == null)
                {
                    // count occurences of x in A, return x or NO
                    int count = 0;
                    foreach (Star s2 in A)
                    {
                        if (distanceBetweenStars(x, s2, d))
                        {
                            count++;
                        }
                    }

                    if (count > A.Count / 2)
                    {
                        return(x);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    // count occurrences of x and y in A, return x, y, or NO
                    int ycount = 0;
                    int xcount = 0;
                    foreach (Star s2 in A)
                    {
                        if (distanceBetweenStars(y, s2, d))
                        {
                            ycount++;
                        }
                        if (distanceBetweenStars(x, s2, d))
                        {
                            xcount++;
                        }
                    }

                    if (xcount > A.Count / 2)
                    {
                        return(x);
                    }

                    else if (ycount > A.Count / 2)
                    {
                        return(y);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: mckayfenn/CS4150
 /**
  * Return true if s1 and s2 are with d of each other, false otherwise
  **/
 public static bool distanceBetweenStars(Star s1, Star s2, long d)
 {
     return((((s1.x - s2.x) * (s1.x - s2.x)) + ((s1.y - s2.y) * (s1.y - s2.y))) <= (d * d));
 }
예제 #3
0
파일: Program.cs 프로젝트: mckayfenn/CS4150
        // not working correctly
        public static Star findMajorityFaster(List <Star> A, long d)
        {
            if (A.Count == 0)
            {
                return(null);
            }
            else if (A.Count == 1)
            {
                return(A.ElementAt(0));
            }
            else
            {
                List <Star> APrime = new List <Star>();
                Star        y      = null;

                // Find A' and y
                for (int i = 0; i < A.Count; i++)
                {
                    if (i + 1 < A.Count)
                    {
                        Star s1 = A.ElementAt(i);
                        Star s2 = A.ElementAt(i + 1);
                        if (distanceBetweenStars(s1, s2, d))
                        {
                            APrime.Add(s1); // if the elements are are same, keep 1 of them in APrime
                        }
                    }
                    else
                    {
                        y = A.ElementAt(i); // if there's a leftover
                    }
                }

                Star x = findMajorityFaster(APrime, d);

                if (x == null)
                {
                    // if A is odd, count occurrences of y in A, return y or NO as appropriate
                    if (A.Count % 2 != 0)
                    {
                        // count occurences of y in A, return y or NO
                        int count = 0;
                        foreach (Star s2 in A)
                        {
                            if (distanceBetweenStars(y, s2, d))
                            {
                                count++;
                            }
                        }

                        if (count > A.Count / 2)
                        {
                            return(y);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null); // else return NO
                    }
                }
                else
                {
                    // count occurences of x in A, return x or NO
                    int count = 0;
                    foreach (Star s2 in A)
                    {
                        if (distanceBetweenStars(x, s2, d))
                        {
                            count++;
                        }
                    }

                    if (count > A.Count / 2)
                    {
                        return(x);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
예제 #4
0
파일: Program.cs 프로젝트: mckayfenn/CS4150
        static void Main(string[] args)
        {
            List <Star>             allStars = new List <Star>();
            Dictionary <long, long> stars    = new Dictionary <long, long>();


            long   d     = 0;
            int    count = 0;
            string line;

            string[] testingData = new string[8];
            testingData[0] = "20 7";
            testingData[1] = "1 1";
            testingData[2] = "100 100";
            testingData[3] = "1 3";
            testingData[4] = "101 101";
            testingData[5] = "3 1";
            testingData[6] = "102 102";
            testingData[7] = "3 3";

            //string[] testingData = new string[5];
            //testingData[0] = "10 4";
            //testingData[1] = "45 46";
            //testingData[2] = "90 47";
            //testingData[3] = "45 54";
            //testingData[4] = "90 43";

            while ((line = Console.ReadLine()) != null)
            //for (int i = 0; i < testingData.Length; i++)
            {
                string[] xy = line.Split(null);
                //string[] xy = testingData.ElementAt(i).Split(null);
                if (count == 0)
                {
                    long.TryParse(xy[0], out d); // get the diameter
                }
                else
                {
                    long x;
                    long y;
                    long.TryParse(xy[0], out x);
                    long.TryParse(xy[1], out y);
                    //stars.Add(x, y);
                    allStars.Add(new Star(x, y));
                }
                count++;
            }

            Star s1 = findMajority(allStars, d);

            int total = 0;

            if (s1 == null)
            {
                Console.WriteLine("NO");
            }
            else
            {
                foreach (Star s2 in allStars)
                {
                    if (distanceBetweenStars(s1, s2, d))
                    {
                        total++;
                    }
                }
            }

            if (total != 0)
            {
                Console.WriteLine(total);
            }
            //Console.ReadLine();
        }
예제 #5
0
        private Star findMajority(List <Star> stars)
        {
            if (stars.Count == 0)
            {
                return(null);
            }
            else if (stars.Count == 1)
            {
                return(stars[0]);
            }
            else
            {
                int         i          = 0;
                List <Star> starsPrime = new List <Star>();
                bool        isOdd      = false;
                while (i <= stars.Count)
                {
                    if (k % 2 != 0)
                    {
                        yStar = stars[stars.Count - 1];
                        //stars.Remove(stars[stars.Count - 1]);
                        isOdd = true;
                    }
                    if ((i + 1) >= stars.Count)
                    {
                        break;
                    }
                    long x1 = stars[i].x;
                    long y1 = stars[i].y;

                    long x2 = stars[i + 1].x;
                    long y2 = stars[i + 1].y;

                    if ((((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))) <= d * d)
                    {
                        starsPrime.Add(stars[i]);
                        //stars.Remove(stars[i + 1]);
                    }
                    else
                    {
                        //stars.Remove(stars[i]);
                        //stars.Remove(stars[i + 1]);
                    }
                    i = i + 2;
                }

                xStar = findMajority(starsPrime);
                if (xStar == null)
                {
                    if (k % 2 != 0)
                    {
                        //int yStarCount = 0;
                        for (int j = 0; j < stars.Count; j++)
                        {
                            long x1 = yStar.x;
                            long y1 = yStar.y;

                            long x2 = stars[j].x;
                            long y2 = stars[j].y;

                            if ((((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))) <= d * d)
                            {
                                yStarCount++;
                            }
                        }
                        if (yStarCount > stars.Count / 2)
                        {
                            return(yStar);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    for (int j = 0; j < stars.Count; j++)
                    {
                        long x1 = xStar.x;
                        long y1 = xStar.y;

                        long x2 = stars[j].x;
                        long y2 = stars[j].y;

                        if ((((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))) <= d * d)
                        {
                            xStarCount++;
                        }
                    }
                    if (xStarCount > stars.Count / 2)
                    {
                        return(xStar);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
예제 #6
0
        private static void Main(string[] args)
        {
            GalaxyQuest gq = new GalaxyQuest();
            string      line;


            string[] test  = { "10 4", "45 46", "90 47", "45 54", "90 43" };
            string[] test2 = { "20 7", "1 1", "100 100", "1 3", "101 101", "3 1", "102 102", "3 3" };

            string[] test4 = { "20 9", "100 100", "1 1", "1 3", "101 101", "2 1", "3 1", "102 102", "3 3", "5000 3000" };

            string[] test3 = { "20 6", "100 100", "1 1", "1 3", "3 1", "3 3", "101 101" };

            string[] test5 = { "10 2", "1 1", "20 20" };

            string[] test6 = { "10 6", "1 1", "20 20", "2 2", "3 3", "50 50", "70 70" };

            string[] test7 = { "10 1", "1 1" };

            string[] test8 = { "20 7", "100 100", "1 1", "1 3", "1010 1010", "2 1", "10200 10200", "3 3" };



            int         count = 1;
            List <Star> stars = new List <Star>();

            while ((line = Console.ReadLine()) != null)
            //for(int l = 0; l < test5.Length; l++)
            {
                String[] splitLine = line.Split(null);
                //String[] splitLine = test5[l].Split(null);
                if (count != 1)
                {
                    stars.Add(new Star(long.Parse(splitLine[0]), long.Parse(splitLine[1])));
                }
                else
                {
                    gq.d = long.Parse(splitLine[0]);
                    gq.k = long.Parse(splitLine[1]);
                }
                count++;
            }
            Star answer = gq.findMajority(stars);

            if (answer == stars[0])
            {
                Console.WriteLine(1);
            }
            else if (answer == null)
            {
                Console.WriteLine("NO");
            }
            else if (answer == gq.xStar)
            {
                Console.WriteLine(gq.xStarCount);
            }
            else
            {
                Console.WriteLine(gq.yStarCount);
            }
            //Console.Read();
        }