LintCode 1136: High Click Induction Area

本文介绍了一种算法,用于在二维矩阵中计算高点击感应区域的数量。通过设定像素阈值和区域阈值,算法能够识别并计数那些连接的、像素值高于阈值且面积大于指定值的区域。提供了两种解法,一种使用BFS进行搜索,另一种使用并查集。
  1. 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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值