6th Maximal Rectangle

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

/*

calc max rect in a matrix

eg:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

res: 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++;
			else
				return 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 location

			for(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));

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值