/*
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;
}
将石头随即放入矩阵中 找出最小SubMatrix包含所有石头
最新推荐文章于 2022-02-23 15:02:25 发布