Smallest rectangle Enclose Black Pixels

本文介绍了一种利用二分搜索算法寻找图像中指定元素最小包围区域的方法。通过定义边界条件,采用递归方式确定目标区域的左、右、上、下边界,实现了对特定像素点周围非零值矩阵的有效搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 public class Solution {
 2     private char[][] image;
 3     public int minArea(char[][] image, int x, int y) {
 4         if (image.length == 0 || image[0].length == 0) {
 5             return 0;
 6         }
 7         this.image = image;
 8         int n = image.length, m = image[0].length;
 9         int left = searchRow(0, x, 0, m, true);
10         int right = searchRow(x + 1, n, 0, m, false);
11         int top = searchCols(0, y, 0, n, true);
12         int bottom = searchCols(y + 1, m, 0, n, false);
13         return (right - left) * (bottom - top);
14     }
15     
16     private int searchCols(int i, int j, int top, int bottom, boolean bound) {
17         while (i != j) {
18             int k = top;
19             int mid = (i + j) / 2;
20             while (k < bottom && image[k][mid] == '0') {
21                 k++;
22             }
23             if(k < bottom == bound) {
24                 j = mid;
25             } else {
26                 i = mid + 1;
27             }
28         }
29         return i;
30     }   
31     
32     private int searchRow(int i, int j, int left, int right, boolean bound) {
33         while (i != j) {
34             int k = left;
35             int mid = (i + j) / 2;
36             while (k < right && image[mid][k] == '0') {
37                 k++;
38             }
39             if(k < right == bound) {
40                 j = mid;
41             } else {
42                 i = mid + 1;
43             }
44         }
45         return i;
46     }
47 }

Binary search:

1. The bound is defined to search "0" or "1". When search 0 - x, we want to get the most left "1". So k < right mean there is a one, j = mid. But when we do x - n search, we want to get most left "0". So when k < right, we still need to keep searching mid + 1.

 

转载于:https://www.cnblogs.com/shuashuashua/p/5645609.html

"SmallestRectangle1"通常是指在计算机科学和算法领域中,寻找二维平面上的最小包围矩形的问题。这个问题可以有多种变体,但核心思想是在给定的一组点或形状中找到能够完全包含这些点或形状的最小矩形。 解决这个问题的方法可以包括以下步骤: 1. **确定点集的范围**: - 找到所有点的最小和最大x坐标以及最小和最大y坐标。 - 这些坐标确定了一个初始的矩形边界。 2. **旋转和计算面积**: - 通过旋转这些点,找到能够使矩形面积最小的旋转角度。 - 计算每个旋转角度下的矩形面积,找到最小值。 3. **使用凸包**: - 对于更复杂的情况,可以使用凸包算法来减少计算量。凸包是一组点集的最小凸多边形。 - 在凸包上应用旋转和面积计算。 以下是一个简单的Python示例代码,展示如何找到二维平面上的一组点的最小包围矩形: ```python import numpy as np def smallest_rectangle(points): # 找到点集的边界 min_x, min_y = np.min(points, axis=0) max_x, max_y = np.max(points, axis=0) # 初始矩形面积 initial_area = (max_x - min_x) * (max_y - min_y) # 旋转角度 angles = np.linspace(0, np.pi/2, 1000) min_area = initial_area best_angle = 0 for theta in angles: # 旋转矩阵 rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) # 旋转后的点 rotated_points = np.dot(points, rotation_matrix) # 旋转后的边界 min_x_rot, min_y_rot = np.min(rotated_points, axis=0) max_x_rot, max_y_rot = np.max(rotated_points, axis=0) # 旋转后的面积 area = (max_x_rot - min_x_rot) * (max_y_rot - min_y_rot) if area < min_area: min_area = area best_angle = theta return min_area, best_angle # 示例点集 points = np.array([[1, 1], [1, 3], [3, 1], [3, 3], [2, 2]]) area, angle = smallest_rectangle(points) print(f"最小面积: {area}, 最佳旋转角度: {angle}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值