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 = 0y = 2,

Return 6.

分析:

  典型Flood Fill泛洪算法的应用,从一个点扩展到整个区域。

代码:

class Solution {
private:
int xmin = INT_MAX, xmax = INT_MIN, ymin = INT_MAX, ymax = INT_MIN;
public:
inline void adjust(int i, int j) {
xmin = min(xmin, i);
xmax = max(xmax, i);
ymin = min(ymin, j);
ymax = max(ymax, j);
return;
}
void dfs(vector<vector<int> > &matrix, int i, int j, vector<vector<bool> > visited) {
if(!matrix[i][j] || visited[i][j])
return;
//调整最大边缘点
adjust(i, j);
visited[i][j] = true;
dfs(matrix, i + , j, visited);
dfs(matrix, i - , j, visited);
dfs(matrix, i, j + , visited);
dfs(matrix, i, j - , visited);
visited[i][j] = false;
return;
}
int minArea(vector<vector<int> > &matrix, int x, int y) {
//设立岗哨,避免递归内部为了判断边界而产生大量开销
matrix.insert(matrix.begin(), vector<int> (matrix[].size(), ));
matrix.push_back(vector<int> (matrix[].size(), ));
for(auto &m : matrix) {
m.insert(m.begin(), );
m.push_back();
}
//避免重复访问
vector<vector<bool> > visited(matrix.size(), vector<bool> (matrix[].size(), false));
dfs(matrix, x + , y + , visited);
return (ymax - ymin + ) * (xmax - xmin + );
}
};

[Locked] Smallest Rectangle Enclosing Black Pixels的更多相关文章

  1. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  2. Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  3. LeetCode Smallest Rectangle Enclosing Black Pixels

    原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...

  4. 302. Smallest Rectangle Enclosing Black Pixels

    题目: An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The b ...

  5. Smallest Rectangle Enclosing Black Pixels 解答

    Question An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. ...

  6. [Swift]LeetCode302. 包含黑色像素的最小矩形 $ Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  7. 【leetcode】302.Smallest Rectangle Enclosing Black Pixels

    原题 An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The bl ...

  8. LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. 手势交互之GestureDetector

    GsetureDetector 一.交互过程 触屏的一刹那,触发MotionEvent事件 被OnTouchListener监听,在onTouch()中获得MotionEvent对象 GestureD ...

  2. vs编译和运行的区

    编译: 是把代码转变成一系列指令(把源代码翻译为计算机能够识别的语言),产生目标代码,并不限于EXE(EXE只是WINDOWS的东西),这样才能装入内存; 运行: 是运行目标代码(运行EXE),就是执 ...

  3. ASP生成新会员编号

    Function MakeUserCode OpenDB() Randomize dim getid_rs,getid set getid_rs=rsobj do while true getid=^ ...

  4. 用sed、awk、grep同时匹配多个条件(与模式、或模式)

    同时匹配ABC 和 123:sed -n '/ABC/{/123/p}' awk '/ABC/&&/123/{ print $0 }' grep -E '(ABC.*123|123.* ...

  5. WCF上传、下载、删除文件

    关键代码: --上传的stream处理,转为bytep[] private void Parse(Stream stream, Encoding encoding) { this.Success = ...

  6. CSS居中的方法总结

    [水平居中] 行内:text-align:center; 定宽块状:1.left:0 right:0然后用margin: auto外边距填充,水平方向不会发生外边距叠加;  2.绝对定位(父元素定位不 ...

  7. Codeforces 441D Valera and Swaps(置换群)

    题意: 给定一个1~n的排列(n<=3000),输出字典序最小且次数最少的交换操作,使得操作后的排列可以通过最少m次交换得到排列[1,2,...n] Solution: 可以将排列的对应关系看做 ...

  8. Ubuntu软件包管理命令全面集锦

    说明:由于图形化界面方法(如Add/Remove... 和Synaptic Package Manageer)比较简单,所以这里主要总结在终端通过命令行方式进行的软件包安装.卸载和删除的方法. 一.U ...

  9. TatukGIS - GisDefs - ChangeDir 函数

    函数名称  ChangeDir 所在单元  GisDefs 函数原型  function ChangeDir(const _path: String): String;   函数说明 如果 _path ...

  10. UFLDL实验报告3:Self-taught

    Self-taught 自我学习器实验报告 1.Self-taught 自我学习实验描述 自我学习是无监督特征学习算法,自我学习意味着算法能够从未标注数据中学习,从而使机器学习算法能够获得更大数量的数 ...