在矩阵c++中查找区域
Find areas in matrix c++
我需要找到矩阵中有多少区域和多少元素。矩阵中填充有0和1。如果有相邻的1(对角线、垂直或水平),我们就有一个区域。
int count_regions( int *arr, int rows, int cols ) {
int region_count = 0;
for ( int first_index = 0; first_index != rows * cols; ++ first_index ) {
if ( arr[ first_index ] == 0 ) continue;
++ region_count;
int first_row = first_index / cols, first_col = first_index % cols;
int last_col;
for ( last_col = first_col;
last_col != cols && arr[ first_row * cols + last_col ] != 0;
++ last_col ) ;
for ( int last_row = first_row;
last_row != rows && arr[ last_row * cols + first_col ] != 0;
++ last_row ) {
for ( int col = first_col; col != last_col; ++ col ) {
arr[ last_row * cols + col ] = 0;
}
}
}
return region_count;
}
试着看看连接组件的标签算法。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- 使用gcc从静态链接的文件中查找可选符号
- 在C++中查找范围的长度
- 算法问题:查找从堆栈中弹出的所有序列
- 查找要填充区域的矩形的大小
- 查找每个连接组件区域的邻域
- 查找一个区域中未包含在另一个区域中的第一个元素
- 用于在黑白图像上查找未连接区域(岛屿)的算法
- 使用区域设置查找字符串中的子字符串
- 在矩阵c++中查找区域
- C程序查找区域
- 在二维数组的矩形区域内查找最大值的快速方法
- 查找排序区域中元素数量的最快方法是什么?