[Locked] 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 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.
分析:
典型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的更多相关文章
- [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 ...
- 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 ...
- LeetCode Smallest Rectangle Enclosing Black Pixels
原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...
- 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 ...
- 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. ...
- [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 ...
- 【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 ...
- 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 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- oracle行列转换总结-转载自ITPUB
原贴地址:http://www.itpub.net/thread-1017026-1-1.html 谢谢原贴大人 最近论坛很多人提的问题都与行列转换有关系,所以我对行列转换的相关知识做了一个总结, 希 ...
- Deep Learning 学习随记(六)Linear Decoder 线性解码
线性解码器(Linear Decoder) 前面第一章提到稀疏自编码器(http://www.cnblogs.com/bzjia-blog/p/SparseAutoencoder.html)的三层网络 ...
- SGU 170.Particles
Solution: 这其实是道很简单的题. 只要从一端开始,以‘+’或‘-’,任意一种开始找,找到与目标串最近的相同字符的距离就是需要交换的次数 ...
- Codeforces 475 D.CGCDSSQ
题目说了a的范围小于10^9次方,可实际却有超过的数据...真是醉了 算出以f[i]结尾的所有可能GCD值,并统计: f[i]可以由f[i-1]得出. /* 递推算出所有GCD值,map统计 */ # ...
- OC文件操作(2)
NSFileManager 文件管理器完成文件的创建.移动.拷贝等管理操作 1.查询文件和目录 OC中查询路径下的目录主要分为浅度遍历和深度遍历. 浅度遍历 NSFileManager * ma ...
- Mac使用rz、sz远程上传下载文件
习惯了 在windows下的securecrt和xshell的rz 和sz. rz 可以很方便的从客户端传文件到服务器,sz也可以很方便的从服务器传文件到客户端,就算中间隔着跳板机也不影响.在mac下 ...
- Hdu 2874 Connections between cities
题意: 城市 道路 没有环 不一定连通的树 求两城市的最短距离 设想一下就是很多小树 那好办 思路: lca离线算法 然后有个技巧就是 每次我们tarjan一棵树不是最后树的节点都访问过并且孩子全 ...
- asp.net将数据导出到excel
本次应用datatable导出,若用gridview(假设gridview设为了分页显示)会出现只导出当前页的情况. protected void btnPrn_Click(object sender ...
- Unity3d脚本的生命周期
接下来,做出一下讲解:最先执行的方法是Awake,这是生命周期的开始,用于进行激活时的初始化代码,一般可以在这个地方将当前脚本禁用:this.enable=false,如果这样做了,则会直接跳转到On ...
- 根据打开页面加载不同Js
根据打开页面加载不同Js //根据打开页面加载不同JS $(document).ready(function(){ var href = document.URL; /*获取当前页面的URL*/ if ...