c++ 代码: 两种解法 ,深度优先搜索(DFS), 广度优先搜索(BFS)
#include
#include
#include
#include
using namespace std;
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)
class Solution
{
int x1 = INT_MAX, x2 = INT_MIN;
int y1 = INT_MAX, y2 = INT_MIN;
public:
int minArea0(vector> &image, int x, int y) // 解法: 深度优先搜索 DFS
{
int r = image.size(), c = image[0].size();
dfs(x, y, r, c, image);
for (auto ele : loc)
{
x1 = min(x1, ele[0]); // 上边界
x2 = max(x2, ele[0]); // 下边界
y1 = min(y1, ele[1]); // 左边界
y2 = max(y2, ele[1]); // 右边界
}
return (x2 - x1 + 1) * (y2 - y1 + 1);
}
int minArea(vector> &image, int x, int y) // 解法: 广度优先搜索 BFS
{
int m = image.size(), n = image[0].size(), i, j, nx, ny, k;
vector> dir = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}}; // 上下左右 方向数组
queue> q;
q.push({x, y});
image[x][y] = '0'; // 访问过了
while (!q.empty())
{
i = q.front()[0];
j = q.front()[1];
q.pop();
x1 = min(x1, i); // 上边界
x2 = max(x2, i); // 下边界
y1 = min(y1, j); // 左边界
y2 = max(y2, j); // 右边界
for (k = 0; k < 4; ++k)
{
nx = i + dir[k][0];
ny = j + dir[k][1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && image[nx][ny] == '1')
{
q.push({nx, ny});
image[nx][ny] = '0'; // 访问过了
}
}
}
return (x2 - x1 + 1) * (y2 - y1 + 1);
}
private:
vector> loc; // image中所有像素值为1的位置集合列表
int vis[102][102];
int dir[4][2] = {
0, 1,
1, 0,
-1, 0,
0, -1};
bool check(int x, int y, int r, int c)
{
if (x < 0 || y < 0 || x >= r || y >= c)
return false;
return true;
}
void dfs(int x, int y, int r, int c, vector> &img)
{
vis[x][y] = 1;
loc.push_back({x, y});
for (int i = 0; i < 4; i++)
{
int nx = x + dir[i][0], ny = y + dir[i][1];
if (check(nx, ny, r, c) && !vis[nx][ny] && img[nx][ny] == '1')
dfs(nx, ny, r, c, img);
}
}
};
int main()
{
int row = 3;
int column = 4;
vector> grid(row, vector(column, 0)); // 初始化row*column二维动态数组,初始化值为0
char A[3][4] = {{'0', '0', '1', '0'},
{'0', '1', '1', '0'},
{'0', '1', '0', '0'}};
for (int m = 0; m < grid.size(); m++)
{
for (int n = 0; n < grid[m].size(); n++)
{
grid[m][n] = A[m][n];
cout << grid[m][n] << " ";
}
cout << "\n";
}
int x = 0, y = 2;
Solution sol = *new Solution();
int res = sol.minArea(grid, x, y);
printf("minArea: %d", res);
return 0;
}
// 编译:g++ demo.cc -o demo
// 执行: ./demo
java代码:二分查找 确定边界
public class Demo {
public int minArea(char[][] image, int x, int y) { // 二分查找
int minRow = searchFirst(image, 0, y, true);
int maxRow = searchLast(image, y, image[0].length - 1, true);
int minCol = searchFirst(image, 0, x, false);
int maxCol = searchLast(image, x, image.length - 1, false);
return (maxRow - minRow + 1) * (maxCol - minCol + 1);
}
private int searchFirst(char[][] image, int start, int end, boolean isVertical) {
int l = start, r = end;
while (l + 1 < r) {
int mid = l + (r - l) / 2;
if (hasBlack(image, mid, isVertical)) {
r = mid;
} else {
l = mid;
}
}
if (hasBlack(image, l, isVertical)) {
return l;
}
return r;
}
private int searchLast(char[][] image, int start, int end, boolean isVertical) {
int l = start, r = end;
while (l + 1 < r) {
int mid = l + (r - l) / 2;
if (hasBlack(image, mid, isVertical)) {
l = mid;
} else {
r = mid;
}
}
if (hasBlack(image, r, isVertical)) {
return r;
}
return l;
}
private boolean hasBlack(char[][] image, int rowOrCol, boolean isVertical) {
if (isVertical) {
for (int i = 0; i < image.length; ++i) {
if (image[i][rowOrCol] == '1') {
return true;
}
}
} else {
for (int i = 0; i < image[0].length; ++i) {
if (image[rowOrCol][i] == '1') {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
char[][] map = new char[][] {{'0','0','1','0'},
{'0','1','1','0'},
{'0','0','1','0'}
};
int x =0, y =2;
Demo d = new Demo();
int area = d.minArea(map, x, y);
System.out.println(area);
}
}
参考链接:
尝试用二分来解决图像处理问题