Cup Blue Bridge - maximum boundary sub-matrix 1 (pre-treatment)

Description Title: Given a matrix N✖N matrix, this matrix, only two kinds of values ​​of 0 and 1, all the border returns the maximum length of a square side length of 1.

E.g:

{0,1,1,1,1},
{0,1,0,0,1},
{0,1,0,0,1},
{0,1,1,1,1},
{0,1,0,1,1}
 其中,边框全是1的最大正方形的大小是4*4,故返回4。

This question before I wrote a search method using violence to solve, but a relatively large time complexity O (N ^ 4) may time out, did not read the small partner may have a look: violence Search version

Here we first of this matrix pretreatment, somewhat similar dynamic programming idea is to create aN✖N✖2The three-dimensional array, wherein a predetermined, if each element is 1, then look at the right and lower sides, respectively, the number of elementscontinuous1, and then use the position number 0 of the final three-dimensional memory array to the right of the (Including itself) Is the number 1, No. 1 of the last dimension able to save the side position (Including itself) Number 1. There should be little skill is initialized from the last element of the last row (i.e., [N-1] [N-1]) forward. Then we check the auxiliary three-dimensional array meets a particular number (eg n-order) to order - check method is very simple, just look at the current element (this is the top-left corner of the border) and the right side whether lower side at least n 1, if the right lower vertex side at least n 1, and if the right side of the lower left apex of at least n-1.

Look at the code it:

#include<iostream>
#define N 5

using namespace std;

int solve(int matrix[][N]){
	 
	int helper[N][N][2]={0};  //申请一个辅助数组空间
	int r = N-1,c = N-1;  //r,c为要预处理的当前位置,从最后一个元素开始
	//先对最后一行初始化 
		for(;c>=0;c--){
			if(matrix[r][c] == 1){   //前提得是1 
				if(c == N-1){  //如果是最后一列 
					helper[r][c][0] = 1;
					helper[r][c][1] = 1;
				}
				else{
					helper[r][c][0] = helper[r][c+1][0] + 1;
					helper[r][c][1] = 1;
				}
			}
		}
		
		//再对前面的预处理
		//防止越界 
		if(N-2 >= 0){ 
			r = N-2;
			for(;r>=0;r--){
				for(c = N-1;c>=0;c--){
					if(matrix[r][c] == 1){   //前提得是1 
						if(c == N-1){  //如果是最后一列 
							helper[r][c][0] = 1;
							helper[r][c][1] = helper[r+1][c][1] + 1;
						}			
						else{
							helper[r][c][0] = helper[r][c+1][0] + 1;
							helper[r][c][1] = helper[r+1][c][1] + 1;
						}
					}
				}
			}
	 	
		}   
	
	int n = N;  //初始阶数为N
	while(n>=0){ 
	for(int i=0;i<N;i++){
		if(i + n > N) break;
		for(int j=0;j<N;j++){
			if(j + n > N) break;
			//check一下左上顶点的右边和下边,右上顶点的下边以及左下顶点的右边是否满足n阶 
			if(helper[i][j][0] >= n && helper[i][j][1] >= n && helper[i][j+n-1][1] >= n && helper[i+n-1][j][0] >= n){
				return n;
			}
		}
	} 
	n--;
}
	 return n;  //全为0的情况 

} 
int main(){
	
	int matrix[N][N] = {
	{0,1,1,1,1},
	{0,1,0,0,1},
	{0,1,0,0,1},
	{0,1,1,1,1},
	{0,1,0,1,1}
	};
	int res = solve(matrix);
	cout<<"result: "<<res;
}

Run the result is the same:
Here Insert Picture Description
we also welcome to test the absence of flaws, I measured the time in addition to matrix is empty except for a small problem, others are normal, if there is a problem please generously pointed out, oh.

Time Complexity Analysis: initialization auxiliary array is N² (N + N²), and when the inspection is N³ (while: N, for✖2: N²), it is only necessary complexity of O (N³) a.
Released two original articles · won praise 0 · Views 49

Guess you like

Origin blog.csdn.net/weixin_43408112/article/details/104315421