Given a binary matrix, find out the maximum size square sub-matrix with all 1
consider the below binary matrix.
0 1 1 0 0
1 1 0 1 0
0 1 1 1 0
1 1 1 1 0
1 1 1 1 1
0 0 0 0 0
Then the result should be
1 1 1
1 1 1
1 1 1
Code:int n, i, j, answer = 0; scanf("%d", &n); for(i = 0; i < n; ++i) { scanf("%s", &mx[i]); for(j = 0; j < n; ++j) if(mx[i][j] == '1') size[i][j] = 1; } for(i = 1; i < n; ++i) for(j = 1; j < n; ++j) { if(mx[i][j] == '1') size[i][j] = min(size[i][j - 1], min(size[i - 1][j], size[i - 1][j - 1])) + 1; if(size[i][j] > answer) answer = size[i][j]; } for(i = 0; i < answer; ++i) { for(j = 0; j < answer; ++j) printf("1"); printf("\n"); }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks