6th Maximal Rectangle

来源:互联网 发布:程序员怎么面试 编辑:程序博客网 时间:2024/05/18 00:26

这里仍旧使用暴力求解法,求出矩阵中的最大的矩形面积。

/*calc max rect in a matrixeg:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0res: 6*/int buf_all[4][5] = {1 ,1 ,1 ,1 ,1 , 1 ,1 ,1 ,1 ,1 , 1 ,1 ,1 ,1 ,1 , 1 ,1 ,0 ,1 ,1};int check_rect(int (*buf)[5],int p_x,int p_y,int off_x,int off_y){int i , j , counter = 0;for(i=p_x;i<p_x + off_x +1;i++)for (j = p_y; j < p_y + off_y+1; j++){/* code */if(buf[i][j])counter++;elsereturn 0;}//printf("check_rect x %d , y %d , off_x %d, off_y %d , counter %d\n",p_x,p_y,off_x,off_y,counter);return counter;}int process(int (*buf)[5] , int len_x , int len_y){int i,j,off_x=1,off_y=1;int max_mat = 0 , temp_mat = 0;for(i=0;i<len_x-1;i++)for(j=0;j<len_y-1;j++){//main loop -- decide the locationfor(off_x = 1;off_x < len_x - i  ;off_x++)for(off_y = 1;off_y < len_y - j  ;off_y++){// offset -- decide the area//printf("log i %d,j %d,off_x %d,off_y %d\n", i,j,off_x,off_y);temp_mat = check_rect(buf,i,j,off_x,off_y);if(temp_mat > max_mat)max_mat = temp_mat;}}return max_mat;}int print_mat(int (*buf)[5]){int i,j;printf("print_mat\n");for(i=0;i<4;i++){for(j=0;j<5;j++){printf("%d\t", buf[i][j]);}printf("\n\n");}}int main(){//print_mat(buf_all);printf("max_mat is %d\n", process(buf_all , 4 ,5));}


原创粉丝点击