Exemplo n.º 1
0
        public bool IsSquareWithBorders(int[,] matrix, int row, int col, int size, SquareBorder bordervalue)
        {
            //Important: Formula for top, bottom, left and right

            //check the top and bottom border of the square in the matrix for the given size starting at given row and colm
            //for size = 5, j = 0, 1,2,3,4
            //matrix is xero based index
            // 00  01 02 03 04
            // 10  11 12 13 14
            // 20  21 22 23 24
            // 30  31 32 33 34
            // 40  41 42 43 44
            //row and col will tell where to start the iteration
            for (int j = 0; j < size; j++)
            {
                //check top border
                if (matrix[row, col + j] != (int)bordervalue)
                {
                    return(false);
                }

                //check bottom border

                if (matrix[(size - 1) + row, col + j] != (int)bordervalue)
                {
                    return(false);
                }
            }

            //here k is from 1 to size -1 bcoz the top and bottom border already covered the first and last of left and right border
            for (int k = 1; k < size - 1; k++)
            {
                //check left border
                if (matrix[k + row, col] != (int)bordervalue)
                {
                    return(false);
                }

                //check right border
                if (matrix[k + row, (size - 1) + col] != (int)bordervalue)
                {
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        //conver the matrix into squarecell [,] where squarecell[i,j] represents a cell and
        //i has count of no of black cells consecutively (important) on the right
        //j has count of no of black cells consecutively (important) on the bottom
        public SquareCell[,] ProcessMatrixToSquareCells(int[,] matrix, SquareBorder bordervalue)
        {
            SquareCell[,] cells = new SquareCell[matrix.GetLength(0), matrix.GetLength(1)];

            //loop through row from last to first  ie bottom to top
            for (int row = matrix.GetLength(0) - 1; row >= 0; row--)
            {
                //loop through col from last to first ie right to left
                for (int col = matrix.GetLength(1) - 1; col >= 0; col--)
                {
                    int rightcount  = 0;
                    int bottomcount = 0;
                    //only process the count if the cell has the required border value
                    //If the cell doesn't have the required border value then reset the count as 0 bcoz we are looking for consecutive border value
                    if (matrix[row, col] == (int)bordervalue)
                    {
                        //increment the value if cell has the bordervalue
                        rightcount++;
                        bottomcount++;

                        //find the no of bordervalue on the right cell of the current cell
                        if (col + 1 < matrix.GetLength(1))
                        {
                            SquareCell previouscell = cells[row, col + 1];
                            rightcount += previouscell.RightCount;
                        }

                        //find the no of bordevalue on the bottom cell of the current cell
                        if (row + 1 < matrix.GetLength(0))
                        {
                            SquareCell bottomcell = cells[row + 1, col];
                            bottomcount += bottomcell.BottomCount;
                        }
                    }

                    cells[row, col] = new SquareCell(rightcount, bottomcount);
                }
            }

            return(cells);
        }