-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximalSquare.c
59 lines (59 loc) · 1.7 KB
/
MaximalSquare.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int maximalSquare(char** matrix, int matrixRowSize, int matrixColSize) {
// maintain a maximum be result, and init it to be 0;
if (matrixRowSize == 0 || matrixColSize == 0)
return 0;
// memorize a array
int **result = (int **)malloc(matrixRowSize * sizeof(int *));
for (int i = 0; i < matrixRowSize; i++){
result[i] = (int *)malloc(matrixColSize * sizeof(int));
}
int s_max = matrix[0][0] - '0';
//find the max from first row and first col
for (int i = 0; i < matrixRowSize; i++){
result[i][0] = matrix[i][0] - '0';
if (result[i][0] == 1)
s_max = 1;
}
for (int i = 1; i < matrixColSize; i++){
result[0][i] = matrix[0][i] - '0';
if (result[0][i] == 1)
s_max = 1;
}
// go over row form 1 to rowSize
for (int row = 1; row < matrixRowSize; row++){
// go over col form 1 to colSize
for (int col = 1; col < matrixColSize; col++){
// if this matrix[row][col] be 1
if (matrix[row][col] == '1' && result[row - 1][col - 1] > 0){
int check_num = sqrt(result[row - 1][col - 1]);
// check matrix[row][col - sqrt(i)] to matrix[row][col - 1]
// and check matrix[row - sqrt(i)] to maxtrix[row - 1][col]
int i = 0;
for (; i < check_num; i++){
if (result[row][col - 1 - i] == 0 || result[row - 1 - i][col] == 0){
break;
}
}
// if one is 0 ,this one is 1
// else be (i + 1)^2
result[row][col] = (i + 1) * (i + 1);
//max refresh
if (s_max < result[row][col])
s_max = result[row][col];
}
else{
result[row][col] = matrix[row][col] - '0';
}
}
}
// return max
return s_max;
}
int main()
{
char *input[] = { "0001", "1101", "1111", "0111", "0111" };
maximalSquare(input, 5, 4);
}