Example #1
0
    public Oceans Recurse(int[,] matrix, int i, int j, int rows, int columns, bool[,] visited)
    {
        var ocean  = new Oceans();
        var ocean1 = new Oceans();
        var ocean2 = new Oceans();
        var ocean3 = new Oceans();
        var ocean4 = new Oceans();

        if ((i == 0) || (j == 0))
        {
            ocean.Pacific = true;
        }

        if ((i == rows - 1) || (j == columns - 1))
        {
            ocean.Atlantic = true;
        }

        if (ocean.Pacific && ocean.Atlantic)
        {
            return(ocean);
        }

        if (visited[i, j])
        {
            return(ocean);
        }

        visited[i, j] = true;


        if (i + 1 < rows && matrix[i + 1, j] <= matrix[i, j])
        {
            ocean1 = Recurse(matrix, i + 1, j, rows, columns, visited);
        }
        if (i - 1 >= 0 && matrix[i - 1, j] <= matrix[i, j])
        {
            ocean2 = Recurse(matrix, i - 1, j, rows, columns, visited);
        }
        if (j + 1 < columns && matrix[i, j + 1] <= matrix[i, j])
        {
            ocean3 = Recurse(matrix, i, j + 1, rows, columns, visited);
        }
        if (j - 1 >= 0 && matrix[i, j - 1] <= matrix[i, j])
        {
            ocean4 = Recurse(matrix, i, j - 1, rows, columns, visited);
        }

        if (ocean1.Pacific || ocean2.Pacific || ocean3.Pacific || ocean4.Pacific || ocean.Pacific)
        {
            ocean.Pacific = true;
        }
        if (ocean1.Atlantic || ocean2.Atlantic || ocean3.Atlantic || ocean4.Atlantic || ocean.Atlantic)
        {
            ocean.Atlantic = true;
        }

        return(ocean);
    }
 /// <summary>
 /// Creates a new instance of GeographicSystems
 /// </summary>
 public GeographicSystems()
 {
     Africa        = new Africa();
     Antarctica    = new Antarctica();
     Asia          = new Asia();
     Australia     = new Australia();
     CountySystems = new CountySystems();
     Europe        = new Europe();
     NorthAmerica  = new NorthAmerica();
     Oceans        = new Oceans();
     SolarSystem   = new SolarSystem();
     SouthAmerica  = new SouthAmerica();
     SpheroidBased = new SpheroidBased();
     World         = new World();
 }
Example #3
0
        public IList <IList <int> > PacificAtlantic(int[][] matrix)
        {
            if (matrix.Length == 0 || matrix[0].Length == 0)
            {
                return(new List <IList <int> >());
            }

            int N = matrix.Length;
            int M = matrix[0].Length;

            var oceans = new Oceans[N, M];
            var queue  = new Queue <(int, int)>();

            bool HasOcean(Oceans val, Oceans ocean) => (val & ocean) == ocean;

            void ProcessQueue(Oceans ocean)
            {
                while (queue.Any())
                {
                    (int i, int j) = queue.Dequeue();
                    oceans[i, j]  |= ocean;
                    //Console.WriteLine($"Cur {i},{j} = {oceans[i,j]}");

                    if (i > 0 && !HasOcean(oceans[i - 1, j], ocean) && matrix[i - 1][j] >= matrix[i][j])
                    {
                        oceans[i - 1, j] |= ocean;
                        queue.Enqueue((i - 1, j));
                        //Console.WriteLine($"{i-1},{j} = {oceans[i-1,j]}");
                    }
                    if (j > 0 && !HasOcean(oceans[i, j - 1], ocean) && matrix[i][j - 1] >= matrix[i][j])
                    {
                        oceans[i, j - 1] |= ocean;
                        queue.Enqueue((i, j - 1));
                        //Console.WriteLine($"{i},{j-1} = {oceans[i,j-1]}");
                    }

                    if (i < N - 1 && !HasOcean(oceans[i + 1, j], ocean) && matrix[i + 1][j] >= matrix[i][j])
                    {
                        oceans[i + 1, j] |= ocean;
                        queue.Enqueue((i + 1, j));
                        //Console.WriteLine($"{i+1},{j} = {oceans[i+1,j]}");
                    }
                    if (j < M - 1 && !HasOcean(oceans[i, j + 1], ocean) && matrix[i][j + 1] >= matrix[i][j])
                    {
                        oceans[i, j + 1] |= ocean;
                        queue.Enqueue((i, j + 1));
                        //Console.WriteLine($"{i},{j+1} = {oceans[i,j+1]}");
                    }
                }
            }

            // Pacific
            for (int i = 0; i < N; i++)
            {
                queue.Enqueue((i, 0));
            }
            for (int j = 0; j < M; j++)
            {
                queue.Enqueue((0, j));
            }
            ProcessQueue(Oceans.Pacific);

            // Atlantic
            for (int i = 0; i < N; i++)
            {
                queue.Enqueue((i, M - 1));
            }
            for (int j = 0; j < M; j++)
            {
                queue.Enqueue((N - 1, j));
            }
            ProcessQueue(Oceans.Atlantic);

            var result = new List <IList <int> >();

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    if (oceans[i, j] == Oceans.Both)
                    {
                        result.Add(new List <int> {
                            i, j
                        });
                    }
                }
            }

            return(result);
        }