在给定的布尔矩阵中查找具有最常见区域大小的区域
给定一个布尔二维数组,大小为N*M 的arr[][] ,其中一组连接的 1 形成一个岛。如果两个单元格在水平、垂直或对角线上彼此相邻,则称它们是连接的。任务是找到所有区域大小最常见的区域的左上角位置。
例子:
Input: arr[][] = {{0, 0, 1, 1, 0},
{1, 0, 1, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 1}}
Output: {1, 0}, {3, 4}
Explanation: There are 3 regions, two with length 1 and the other with length 4. So the length of most common region is 1 and the positions of those regions are {1, 0}, {3, 4}.
Input: arr[][] = {{0, 0, 1, 1, 0},
{0, 0, 1, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 1}}
Output: {0, 2}
Explanation: There are 2 regions, one with length 1 and the other with 4. Since both the regions have same frequency, both are the most common region. Hence, print position of any one of the regions.
方法:这个想法是基于在布尔 2D 矩阵中找到岛数的问题。这个想法是将区域的大小及其左上角位置存储在哈希图中。然后遍历 hashmap 找到最常见的区域并打印所需的区域。请按照以下步骤解决问题:
- 初始化一个哈希图 存储区域的大小及其左上角位置。
- 维护一个访问过的数组以跟踪所有访问过的单元格。
- 逐行遍历给定的二维数组,如果当前元素为“1”且未被访问,则从此节点执行 DFS 遍历。遍历后,将区域的大小存储在地图中。
- 完成上述步骤后,遍历地图,找到最常见的区域,然后打印地图中key对应的所有位置。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
#define ROW 4
#define COL 5
// Function to check if a given cell
// can be included in DFS
int isSafe(int M[][COL], int row, int col,
bool visited[][COL])
{
return (row >= 0)
&& (row < ROW) && (col >= 0)
&& (col < COL)
&& (M[row][col]
&& !visited[row][col]);
}
// Utility function to do DFS for a 2D
// boolean matrix
void DFS(int M[][COL], int row, int col,
bool visited[][COL], int& count)
{
// Initialize arrays to get row and column
// numbers of 8 neighbours of a given cell
static int rowNbr[]
= { -1, -1, -1, 0, 0, 1, 1, 1 };
static int colNbr[]
= { -1, 0, 1, -1, 1, -1, 0, 1 };
// Mark this cell as visited
visited[row][col] = true;
// Recur for all connected neighbours
for (int k = 0; k < 8; ++k) {
if (isSafe(M, row + rowNbr[k],
col + colNbr[k], visited)) {
// Increment region length by one
count++;
DFS(M, row + rowNbr[k], col + colNbr[k],
visited, count);
}
}
}
// Function to find all the regions with most
// common length in the given boolean 2D matrix
void commonRegion(int M[][COL])
{
// Make a bool array to mark visited cells,
// initially all cells are unvisited
bool visited[ROW][COL];
memset(visited, 0, sizeof(visited));
// Map to store the size and the regions
unordered_map > > um;
// Traverse through the
// all cells of given matrix
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
// If the current cell is 1 and is
// not visited
if (M[i][j] && !visited[i][j]) {
// Increment count by 1
int count = 1;
// Perform DFS
DFS(M, i, j, visited, count);
um[count].push_back({ i, j });
}
}
}
// Store the most common region length
int mostCommonRegion = 0;
// Traverse the map to find the most common
// region length
for (auto it = um.begin(); it != um.end(); it++) {
int x = it->second.size();
mostCommonRegion = max(mostCommonRegion, x);
}
// Traverse the map to print the regions
// having most common length
for (auto it = um.begin(); it != um.end(); it++) {
int x = it->second.size();
if (mostCommonRegion == x) {
// Print the top left position
// of the regions
for (int i = 0; i < it->second.size(); i++) {
int x = it->second[i][0];
int y = it->second[i][1];
cout << "{" << x << ", " << y << "}, ";
}
break;
}
}
}
// Driver code
int main()
{
// Given Input
int arr[][COL] = { { 0, 0, 1, 1, 0 },
{ 1, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1 } };
// Function call
commonRegion(arr);
return 0;
}
{1, 0}, {3, 4},
时间复杂度: O(N*M)
辅助空间: O(N*M)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。