Lengthy solution, almost beat 100%, iterative BFS, simple integer stack


  • 0
    I
    public class Solution {
        public char[,] UpdateBoard(char[,] board, int[] click) {
            var i = click[0];
            var j = click[1];
            var b = board;
            if(b[i,j] == 'M')
            {
                b[i,j] = 'X';
                return b;
            }
            var s = new Stack<int>();
            s.Push(j);
            s.Push(i);
            while(s.Count!=0)
            {
                i = s.Pop();
                j = s.Pop();
                if(IsBlank(b, i, j))
                {
                    if(i > 0 && j > 0 && b[i-1, j-1] == 'E')
                    {
                        s.Push(j-1);
                        s.Push(i-1);
                    }
                    if(j > 0 && b[i,j-1] == 'E')
                    {
                        s.Push(j-1);
                        s.Push(i);
                    }
                    if(i + 1 < b.GetLength(0) && j > 0 && b[i+1, j-1] == 'E')
                    {
                        s.Push(j-1);
                        s.Push(i+1);
                    }
                    if(i > 0 && b[i-1,j] == 'E')
                    {
                        s.Push(j);
                        s.Push(i-1);
                    }
                    if(i + 1 < b.GetLength(0) && b[i+1,j] == 'E')
                    {
                        s.Push(j);
                        s.Push(i+1);
                    }
                    if(i > 0 && j + 1 < b.GetLength(1) && b[i-1,j+1] == 'E')
                    {
                        s.Push(j+1);
                        s.Push(i-1);
                    }
                    if(j + 1 < b.GetLength(1) && b[i, j+1] == 'E')
                    {
                        s.Push(j+1);
                        s.Push(i);
                    }
                    if(i + 1 < b.GetLength(0) && j + 1 < b.GetLength(1) && b[i+1,j+1] == 'E')
                    {
                        s.Push(j+1);
                        s.Push(i+1);
                    }
                }
            }
            return b;
        }
        
        private bool IsBlank(char[,] b, int i, int j)
        {
            int count = 0;
            
            if(i > 0 && j > 0 && b[i-1, j-1] == 'M')
               count++;
            if(j > 0 && b[i,j-1] == 'M')
               count++;
            if(i + 1 < b.GetLength(0) && j > 0 && b[i+1, j-1] == 'M')
               count++;
            if(i > 0 && b[i-1,j] == 'M')
               count++;
            if(i + 1 < b.GetLength(0) && b[i+1,j] == 'M')
               count++;
            if(i > 0 && j + 1 < b.GetLength(1) && b[i-1,j+1] == 'M')
               count++;
            if(j + 1 < b.GetLength(1) && b[i, j+1] == 'M')
               count++;
            if(i + 1 < b.GetLength(0) && j + 1 < b.GetLength(1) && b[i+1,j+1] == 'M')
               count++;
            
            b[i,j] = count == 0 ? 'B' : (char)(count + (int)'0');
            
            return b[i,j] == 'B';
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.