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. HTML5 TypeArray和Unicode 字符之间转换

    1.Uint32Array测试成功 // Uint32Array 测试成功 //字符串转为ArrayBuffer对象 function strToab() { var str = '张三丰'; var ...

  2. 详细查看数据库SQL执行计划

    DBCC DROPCLEANBUFFERS 清除数据缓存DBCC FREEPROCCACHE  清除执行计划缓存 SET SHOWPLAN_XML ON 此语句导致 SQL Server 不执行 Tr ...

  3. Spot light工具集

    Spot light on UNIX 安装没什么问题 Spot light on Oracle  必须安装32位的客户端,不然搞死你 两者的界面都是吊炸天啊

  4. Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志

    一.spring aop execution表达式说明 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义 ...

  5. 你好,C++(2)1.3 C++世界版图1.4 如何学好C++

    1.3  C++世界版图 C++语言的发展过程,不仅是一个特性不断增加.内容不断丰富的过程,更是一个在应用领域中不断攻城略地的过程.在其30余年的发展过程中,C++在多个应用领域都得到了广泛的应用和发 ...

  6. linux之uniq

    Linux命令uniq的作用是过滤重复部分显示文件内容,这个命令读取输入文件,并比较相邻的行.在正常情况下,第二个及以后更多个重复行将被删去,行 比较是根据所用字符集的排序序列进行的.该命令加工后的结 ...

  7. js数学方法应用

    找出数组中最大的数 var values = [1, 2, 3, 4, 5, 6, 7, 8]; alert(Math.min.apply(Math,values))//8 这个技巧的关键是把 Mat ...

  8. 一起来背ABC

    construction 构造,结构 constructor  构造函数,施工员

  9. Silverlight开发工具汇总

    随着Silverlight技术的逐步完善,Silverlight应用大批的涌现,近期的2010年冬季奥运会,Silverlight作为首选视频播放技术,为全球提供在线赛事实况. Silverlight ...

  10. hadoop输出统计