- High Click Induction Area
On a two-dimensional matrix, every position is a integer pixel value between 00 and 1010.
The position that is larger than pixelThresholdpixelThreshold is called high click induction position.
The area of connected high click positions which is larger than areaThresholdareaThreshold is called high click induction area.
The positions which are adjacent positions are connected.
Please count the number of high click induction area on the two-dimensional matrix.
Example
Sample
Input:
matrix = [[6,2,8],[1,3,7],[6,9,5]]
pixedThreshold = 5
areaThreshold = 1
Output:
2
Clarification
There are three parts of connected high click induction positions:
[(0, 0)]
[(0, 2), (1, 2)]
[(2, 0), (2, 1)]
Two parts of them are which area is larger than 11.
Notice
The length nn and width mm of this matrix are satisfing 1 \le n, m \le 3001≤n,m≤300.
0 \le matrix[x][y] \le 100≤matrix[x][y]≤10
0 \le pixelThreshold \le 100≤pixelThreshold≤10
0 \le areaThreshold \le n * m0≤areaThreshold≤n∗m
这题类似最大岛屿那道题。
解法1:BFS。
struct Node {
int row;
int col;
Node(int r = 0, int c = 0) : row(r), col(c) {}
};
class Solution {
public:
/**
* @param matrix: a two-dimensional matrix.
* @param pixelThreshold: the pixel threshold judging high click position.
* @param areaThreshold: the pixed threshold judging high click area.
* @return: return the number of high click areas.
*/
int highClickAreaCount(vector<vector<int>> &matrix, int pixelThreshold, int areaThreshold) {
int nRow = matrix.size();
if (nRow == 0) return 0;
int nCol = matrix[0].size();
if (nCol == 0) return 0;
int count = 0;
for (int i = 0; i < nRow; ++i) {
for (int j = 0; j < nCol; ++j) {
int islandSize = 0;
if (matrix[i][j] > pixelThreshold) {
islandSize = bfs(matrix, i, j, pixelThreshold);
if (islandSize > areaThreshold) {
count++;
}
}
}
}
return count;
}
private:
int bfs(vector<vector<int>> &matrix, int row, int col, int pixelThreshold) {
int nRow = matrix.size();
int nCol = matrix[0].size();
int islandSize = 1;
queue<Node> q;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
q.push(Node(row, col));
matrix[row][col] = -1;
while(!q.empty()) {
Node currNode = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int newRow = currNode.row + dx[k];
int newCol = currNode.col + dy[k];
if (newRow >= 0 && newRow < nRow && newCol >= 0 && newCol < nCol && matrix[newRow][newCol] > pixelThreshold) {
matrix[newRow][newCol] = -1;
q.push(Node(newRow, newCol));
islandSize++;
}
}
}
return islandSize;
}
};
解法2:并查集
解法3:DFS
本文介绍了一种算法,用于在二维矩阵中计算高点击感应区域的数量。通过设定像素阈值和区域阈值,算法能够识别并计数那些连接的、像素值高于阈值且面积大于指定值的区域。提供了两种解法,一种使用BFS进行搜索,另一种使用并查集。
1513

被折叠的 条评论
为什么被折叠?



