原题

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.

样例:

given the following image:

[

"0010",

"0110",

"0100"

]

and x = 0, y = 2,

Return 6.

解析

求最小覆盖矩形面积

有一个矩阵,由0,1组成,每个1表示一个黑色像素点,

给出第一个黑色像素的位置,求最小的可以覆盖这个矩阵中黑色像素点的矩形大小

思路

一:遍历整个矩阵(缺点,如果矩阵很大,像素点占比很少,则比较浪费)

二:遍历像素点

找出最大最小的x,最大最小的y然后计算矩阵大小

解法

BFS广度有限算法(Queue)

用给出的一个像素点,利用queue,入队第一个像素点,然后入队它上下左右的像素;当队不空时继续遍历,直到所有的像素都遍历一遍

public int minAreaBFS(int[][] image, int x, int y) {
if (image == null || image.length <= 0 || image[0] == null || image[0].length <= 0) {
return 0;
}
int minX = x, minY = y, maxX = x, maxY = y;
Queue<Point> queue = new LinkedList<>();
Point rootP = new Point(x, y);
queue.offer(rootP);
//队不空时就继续
while (!queue.isEmpty()) {
Point p = queue.poll();
minX = Math.min(p.x, minX);
minY = Math.min(p.y, minY);
maxX = Math.max(p.x, maxX);
maxY = Math.max(p.y, maxY);
//判断四周是否有像素,有就入队
check(p.x - 1, p.y, image, queue);
check(p.x + 1, p.y, image, queue);
check(p.x, p.y - 1, image, queue);
check(p.x, p.y + 1, image, queue);
}
return (maxX - minX + 1) * (maxY - minY + 1);
} private void check(int x, int y, int[][] image, Queue queue) {
int maxR = image.length;
int maxC = image[0].length;
Point p = new Point(x, y);
if (x >= 0 && x < maxR && y >= 0 && y < maxC && image[x][y] == 1) {
//遍历过的点置0
image[x][y] = 0;
queue.offer(p);
}
}

DFS深度优先算法(递归)

用给出的第一个点,递归遍历它的上下左右像素,直到遍历完所有的像素点

public int minAreaDFS(int[][] image, int x, int y) {
if (image == null || image.length <= 0 || image[0] == null || image[0].length <= 0) {
return 0;
}
//数组记录最小x,最小y,最大x,最大y
int[] res = {x, y, x, y};
dfs(x, y, image, res);
return (res[2] - res[0] + 1) * (res[3] - res[1] + 1);
} private void dfs(int x, int y, int[][] image, int[] res) {
int maxR = image.length;
int maxC = image[0].length;
//若当前点存在且为像素,才继续,否则返回
if (x >= 0 && x < maxR && y >= 0 && y < maxC && image[x][y] == 1) {
image[x][y] = 0;
res[0] = Math.min(x, res[0]);
res[1] = Math.min(y, res[1]);
res[2] = Math.max(x, res[2]);
res[3] = Math.max(y, res[3]);
dfs(x - 1, y, image, res);
dfs(x + 1, y, image, res);
dfs(x, y - 1, image, res);
dfs(x, y + 1, image, res);
}
}

最新文章

  1. Smokeping -- 监控网络质量
  2. 解决VS2015启动界面卡在白屏的处理方法
  3. [Openwrt 项目开发笔记]:Openwrt平台搭建(一)
  4. 解决你的开发烦恼——Aoite 开源前奏
  5. SqlCommandBuilder实现大数据更新
  6. 第 29 章 CSS3 弹性伸缩布局[上]
  7. KEngine:Unity3D资源的打包、加载、调试监控
  8. Yii源码阅读笔记(二十七)
  9. 四个排名函数(row_number、rank、dense_rank和ntile)的比较
  10. 分享一款简洁的jQuery轮播源码
  11. 在其他的电脑上配置绿色Jre+tomcat运行环境
  12. 基于Processing的数据可视化
  13. JAVA - 优雅的记录日志(log4j实战篇) (转)
  14. Error when sending message to topic test with key: null, value: 2 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
  15. 纯HTML5APP与原生APP的差距在哪?
  16. 安卓开发笔记(二十八):仿写IOS switch选择器控件实现,checkbox
  17. 生产环境中学习Redis
  18. C#之Invoke学习
  19. python练习题-day25
  20. idea调试代码跟踪到tomcat代码里面

热门文章

  1. matlab学习——04图与网络(最短路,最小生成树,最大流)
  2. Jsoup-简单爬取知乎推荐页面(附:get_agent())
  3. pycharm调用shell命令
  4. 第十八章 并发登录人数控制——《跟我学Shiro》
  5. vue项目使用keep-alive的作用
  6. 【miscellaneous】基于gstreamer的实时转码
  7. redis的主从复制和哨兵模式
  8. 高阶函数 filter map reduce
  9. keras损失函数详解
  10. 2019版UI学习路线(含大纲+视频+工具+网盘+面试题)