private static int GetMinminutes(int[][] grid)
        {
            int rows = grid.Length;

            if (rows == 0)
            {
                return(0);
            }

            List <int[]> dirs = new List <int[]>()
            {
                new[] { 0, 1 }, new[] { 1, 0 }, new[] { 0, -1 }, new[] { -1, 0 }
            };
            int minutes = 0, cols = grid[0].Length, freshOrange = 0;
            Queue <Cordinate> rottenQueue = new Queue <Cordinate>();

            for (int i = 0; i < grid.Length; i++)
            {
                for (int j = 0; j < grid[0].Length; j++)
                {
                    if (grid[i][j] == 2)
                    {
                        rottenQueue.Enqueue(new Cordinate(i, j));
                    }
                    if (grid[i][j] == 1)
                    {
                        freshOrange++;
                    }
                }
            }
            Console.WriteLine("Total rottenQueue :{0}, and freshOrange :{1}", rottenQueue.Count, freshOrange);

            while (rottenQueue.Count > 0 && freshOrange > 0)
            {
                minutes++;
                int qSize = rottenQueue.Count;
                for (int i = 0; i < qSize; i++)
                {
                    Cordinate cor = rottenQueue.Dequeue();
                    int       x   = cor.x;
                    int       y   = cor.y;

                    PrintMatrix(grid);
                    foreach (var dir in dirs)
                    {
                        int xNew = x + dir[0];
                        int yNew = y + dir[1];

                        if (IsValidOrage(grid, xNew, yNew, rows, cols))
                        {
                            grid[xNew][yNew] = 2;
                            rottenQueue.Enqueue(new Cordinate(xNew, yNew));
                            freshOrange--;
                        }
                    }
                }
            }
            return(freshOrange == 0 ? minutes : -1);
        }
        private static int TreasureIslandIIDemo(char[][] grid)
        {
            int rows = grid.Length;

            if (rows == 0)
            {
                return(0);
            }

            int cols = grid[0].Length, steps = 0;

            bool[,] visited = new bool[rows, cols];
            int[][] dirs = new[] { new[] { 1, 0 }, new[] { 0, 1 }, new[] { -1, 0 }, new[] { 0, -1 } };

            Queue <Cordinate> queue = new Queue <Cordinate>();

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    if (grid[i][j] == 'S')
                    {
                        queue.Enqueue(new Cordinate(i, j));
                        visited[i, j] = true;
                    }
                }
            }

            // No loop from all the starting points.
            while (queue.Count > 0)
            {
                steps++;
                int qSize = queue.Count;
                for (int i = 0; i < qSize; i++)
                {
                    Cordinate cor = queue.Dequeue();
                    int       x   = cor.x;
                    int       y   = cor.y;
                    PrintMatrix(visited);

                    foreach (var dir in dirs)
                    {
                        int xNew = x + dir[0];
                        int yNew = y + dir[1];
                        if (isSafe(grid, visited, xNew, yNew, rows, cols))
                        {
                            if (grid[xNew][yNew] == 'X')
                            {
                                return(steps);
                            }
                            visited[xNew, yNew] = true;
                            queue.Enqueue(new Cordinate(xNew, yNew));
                        }
                    }
                }
            }

            return(-1);
        }
        private static int TreasureIslandBFS(char[][] grid)
        {
            int rows = grid.Length, steps = 0;

            if (rows == 0)
            {
                return(steps);
            }

            int cols = grid[0].Length;
            Queue <Cordinate> queue = new Queue <Cordinate>();

            queue.Enqueue(new Cordinate(0, 0));
            bool[,] visited = new bool[rows, cols];
            visited[0, 0]   = true;
            int[][] directions = new[] { new[] { 1, 0 }, new[] { 0, 1 }, new[] { -1, 0 }, new[] { 0, -1 } };

            //Start BFS
            while (queue.Count > 0)
            {
                int levelSize = queue.Count;
                for (int i = 0; i < levelSize; i++)
                {
                    Cordinate cor = queue.Dequeue();
                    int       x   = cor.x;
                    int       y   = cor.y;
                    PrintMatrix(visited);
                    foreach (var dir in directions)
                    {
                        int newX = x + dir[0];
                        int newY = y + dir[1];
                        if (isValid(grid, visited, newX, newY, rows, cols))
                        {
                            if (grid[newX][newY] == 'X')
                            {
                                return(steps);
                            }
                            visited[newX, newY] = true;
                            queue.Enqueue(new Cordinate(newX, newY));
                        }
                    }
                }
                steps++;
            }

            return(-1);
        }
Example #4
0
        private static int FindCutOff(IList <IList <int> > forest, int sr, int sc, int tx, int ty, int rows, int cols, int[][] dirs)
        {
            bool[,] seen = new bool[rows, cols];
            Queue <Cordinate> treeQueue = new Queue <Cordinate>();

            treeQueue.Enqueue(new Cordinate(sr, sc));
            int step = 0;

            while (treeQueue.Count > 0)
            {
                step++;
                int qsize = treeQueue.Count;
                for (int i = 0; i < qsize; i++)
                {
                    Cordinate cor = treeQueue.Dequeue();
                    int       x   = cor.x;
                    int       y   = cor.y;

                    foreach (var dir in dirs)
                    {
                        int newX = x + dir[0];
                        int newY = y + dir[1];
                        if (x == newX && y == newY)
                        {
                            return(step);
                        }
                        if (newX >= 0 && newY >= 0 && newX < rows && newY < cols && forest[newX][newY] > 1 && seen[newX, newY] == false)
                        {
                            seen[newX, newY] = true;
                            treeQueue.Enqueue(new Cordinate(newX, newY));
                        }
                    }
                }
            }
            return(-1);
        }
        private static int GetMinHours(int[][] grid)
        {
            int rows = grid.Length;

            if (rows == 0)
            {
                return(0);
            }
            int cols  = grid[0].Length;
            int hours = 0;

            List <int[]> dirs = new List <int[]> {
                new int[] { 0, 1 }, new int[] { 1, 0 }, new int[] { 0, -1 }, new int[] { -1, 0 }
            };

            Queue <Cordinate> zomQueue = new Queue <Cordinate>();
            int humCount = 0;

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    if (grid[i][j] == 1)
                    {
                        zomQueue.Enqueue(new Cordinate(i, j));
                    }
                    else
                    {
                        humCount++;
                    }
                }
            }

            Console.WriteLine("Total zombie :{0}, and human :{1}", zomQueue.Count, humCount);

            while (zomQueue.Count > 0 && humCount > 0)
            {
                hours++;
                int qSize = zomQueue.Count;
                for (int i = 0; i < qSize; i++)
                {
                    Cordinate cor = zomQueue.Dequeue();
                    int       x   = cor.x;
                    int       y   = cor.y;

                    PrintMatrix(grid);
                    foreach (var dir in dirs)
                    {
                        int xNew = x + dir[0];
                        int yNew = y + dir[1];

                        if (IsValidHuman(grid, xNew, yNew, rows, cols))
                        {
                            grid[xNew][yNew] = 1;
                            zomQueue.Enqueue(new Cordinate(xNew, yNew));
                            humCount--;
                        }
                    }
                }
            }

            return(humCount == 0 ? hours : -1);
        }