private void button4_Click(object sender, EventArgs e) { //Given a nxn square matrix //construct a 5*5 square matrix int[,] matrix = new int[, ] { { 0, 1, 1, 0, 1 }, { 1, 1, 0, 1, 0 }, { 0, 1, 1, 1, 0 }, { 1, 1, 1, 1, 0 }, { 1, 1, 1, 1, 1 } }; SubSquare result = FindMaxSubSquare(matrix); StringBuilder sb = new StringBuilder(); if (result != null) { sb.Append("Row vaue: ").Append(result.Row).Append(" , "); sb.Append("Col vaue: ").Append(result.Col).Append(" , "); sb.Append("Size: ").Append(result.Size); } else { sb.Append("No squares found"); } this.textBox8.Text = sb.ToString(); }
public SubSquare FindMaxSubSquare(int[,] matrix) { //Logic is go from max size to 0..we need to find the max subsquare //For a given 5 * 5 matrix, max square is of size 5 and min is 1*1 //size 5,4,3,2,1 for (int i = matrix.GetLength(0); i >= 1; i--) { SubSquare result = FindSquareBySize(matrix, i); if (result != null) { return(result); } } return(null); }
public SubSquare FindMaxSubSquare2(int[,] matrix) { //Logic is go from max size to 0..we need to find the max subsquare //For a given 5 * 5 matrix, max square is of size 5 and min is 1*1 //size 5,4,3,2,1 //process the matrix SquareCell[,] processedmatrix = ProcessMatrixToSquareCells(matrix, SquareBorder.Black); for (int i = matrix.GetLength(0); i >= 1; i--) { SubSquare result = FindSquareBySize2(processedmatrix, i); if (result != null) { return(result); } } return(null); }
private void button5_Click(object sender, EventArgs e) { //Given a nxn square matrix //construct a 5*5 square matrix //This sln is an continuation of previous sln where isSquare() has special logic to check square that has specified border value int[,] matrix = new int[, ] { { 0, 1, 1, 0, 1 }, { 1, 1, 0, 1, 0 }, { 0, 1, 1, 1, 0 }, { 1, 1, 1, 1, 0 }, { 1, 1, 1, 1, 1 } }; //update: we can do this same for rectangel..just take care for max area and then count formula for both row and colum //int[,] matrix = new int[,] {{0,1,0}, // {1,1,0}, // {1,1,0}}; SubSquare result = FindMaxSubSquare2(matrix); StringBuilder sb = new StringBuilder(); if (result != null) { sb.Append("Row vaue: ").Append(result.Row).Append(" , "); sb.Append("Col vaue: ").Append(result.Col).Append(" , "); sb.Append("Size: ").Append(result.Size); } else { sb.Append("No squares found"); } this.textBox8.Text = sb.ToString(); }
/** * Counts number of digits still free * for a specific cell. * * @param boardCell */ private void countDigitsStillFree(BoardCell boardCell) { int[] digitsStillFree = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int emptyCellsNumber = 0; for (int j = 0; j < BOARD_SIZE; j++) { int boardDigit = sudokuBoard[boardCell.rowIndex, j]; if (boardDigit != CELL_EMPTY) { digitsStillFree[boardDigit] = DIGIT_IN_USE; } else if (j != boardCell.colIndex) { emptyCellsNumber++; } } for (int i = 0; i < BOARD_SIZE; i++) { int boardDigit = sudokuBoard[i, boardCell.colIndex]; if (boardDigit != CELL_EMPTY) { digitsStillFree[boardDigit] = DIGIT_IN_USE; } else if (i != boardCell.rowIndex) { emptyCellsNumber++; } } SubSquare sub = SubSquare.getSubSqare(boardCell); /* * Mark digits used in a sub-square. */ for (int i = sub.rowMin; i < sub.rowMax; i++) { for (int j = sub.colMin; j < sub.colMax; j++) { int boardDigit = sudokuBoard[i, j]; if (boardDigit != CELL_EMPTY) { digitsStillFree[boardDigit] = DIGIT_IN_USE; } else if ((i != boardCell.rowIndex) && (j != boardCell.colIndex)) { emptyCellsNumber++; } } } /* * Find number of still free digits to use. */ digitsStillFree[boardCell.digit] = 0; boardCell.digitsStillFreeNumber = emptyCellsNumber; for (int digit = 1; digit < 10; digit++) { if (digitsStillFree[digit] == DIGIT_STILL_FREE) { boardCell.digitsStillFreeNumber++; } } }