[LeetCode][Search] 302. Smallest Rectangle Enclosing Black Pixels

Problem

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

For example, given the following image:

[
  "0010",
  "0110",
  "0100"
]

and x = 0, y = 2,
Return 6.

Solution

DFS搜索所有与当前1连通的点。minX, maxX, minY, maxY用来记录轮廓,最后计算出rectangle的大小。

class Solution {
private:
    int minX, maxX, minY, maxY;
    vector<vector<bool>> canUse;
    int step[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
    void search(vector<vector<char>>& image, int x, int y) {
        canUse[x][y] = false;
        minX = min(minX, x);
        maxX = max(maxX, x);
        minY = min(minY, y);
        maxY = max(maxY, y);
        for(int i = 0; i < 4; i++) {
            int newX = step[i][0] + x;
            int newY = step[i][1] + y;
            if (0 <= newX && newX < image.size() && 0 <= newY && newY < image[0].size() 
            && canUse[newX][newY] && image[newX][newY] == '1') {
                search(image, newX, newY);
            }
        }
    }
    
    int minArea(vector<vector<char>>& image, int x, int y) {
        if (image.size() == 0) {
            return 0;
        }
        
        minX = minY = INT_MAX;
        maxX = maxY = INT_MIN;
        vector<bool> a(image[0].size(), true);
        for(int i = 0; i < image.size(); i++) {
            canUse.push_back(a);
        }
        search(image, x, y);
        
        return (maxX - minX + 1) * (maxY - minY + 1);
    }
};

还有一种BST的方法,参考这里。主要思想是分别对行和列BS,例如对行search,每次要搜索所有元素找到只要有一个为1就算连通。具体参考代码如下:

class Solution {
public:
    int searchX(vector<vector<char>>& image, int beg, int end, bool findMin) {
        if (beg > end) {
            return -1;
        }
        
        int mid = beg + (end - beg) / 2;
        for(int i = 0; i < image[mid].size(); i++) {
            if (image[mid][i] == '1') {
                int index = findMin 
                ? searchX(image, beg, mid - 1, findMin)
                : searchX(image, mid + 1, end, findMin);
                return index != -1 ? index : mid;
            }
        }
        
        return findMin
        ? searchX(image, mid + 1, end, findMin)
        : searchX(image, beg, mid - 1, findMin);
    }
    
    int searchY(vector<vector<char>>& image, int beg, int end, bool findMin) {
        if (beg > end) {
            return -1;
        }
        
        int mid = beg + (end - beg) / 2;
        cout << "beg:" << beg << endl;
        cout << "end:" << end << endl;
        cout << "mid:" << mid << endl;
        for(int i = 0; i < image.size(); i++) {
            if (image[i][mid] == '1') {
                int index = findMin 
                ? searchY(image, beg, mid - 1, findMin)
                : searchY(image, mid + 1, end, findMin);
                return index != -1 ? index : mid;
            }
        }
        
        return findMin
        ? searchY(image, mid + 1, end, findMin)
        : searchY(image, beg, mid - 1, findMin);        
    }
    
    int minArea(vector<vector<char>>& image, int x, int y) {
        if (image.size() == 0 || image[0].size() == 0) {
            return 0;
        }
        int minX = searchX(image, 0, x, true);
        int maxX = searchX(image, x, image.size() - 1, false);
        int minY = searchY(image, 0, y, true);
        int maxY = searchY(image, y, image[0].size() - 1, false);
        
        return (maxX - minX + 1) * (maxY - minY + 1);
    }
};
    原文作者:楷书
    原文地址: https://www.jianshu.com/p/af78afda1777
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞