将石头随即放入矩阵中 找出最小SubMatrix包含所有石头

/*
Consider you have a grid of size m x n. 
There are stones placed randomly in some of the squares of this grid. Design a way to find out minimum rectangular area which covers all the stones in this grid.
*/
#include <cstdlib>
#include <algorithm>

#define R 5
#define C 4


int main()
{
	int mat[R][C] = {{0, 0, 0, 0},
					 {0, 1, 0, 0},
					 {0, 0, 0, 1},
					 {1, 0, 0, 0},
					 {0, 0, 0, 0}};


	int i1 = R - 1, i2 = 0, j1 = C - 1, j2 = 0;

	for(int i = 0; i < R; i++)
		for(int j = 0; j < C; j++)
			if(mat[i][j] == 1)
			{
				i1 = std::min(i1, i);
				i2 = std::max(i2, i);
				j1 = std::min(j1, j);
				j2 = std::max(j2, j);
			}

	printf("(%d, %d), (%d, %d), (%d, %d), (%d, %d)", i1, j1, i1, j2, i2, j1, i2, j2);
	return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值