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

来源:互联网 发布:mac按哪个键切换输入法 编辑:程序博客网 时间:2024/05/16 11:41
/*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 4int 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;}

原创粉丝点击