您的位置:首页 > 其它

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

2012-07-23 11:53 225 查看
/*
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c
相关文章推荐