[CareerCup] 18.11 Maximum Subsquare 最大子方形

简介:

18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels.

LeetCode上的原题,请参见我之前的解法Maximal Square。书上给了两种解法,但是比较长:

解法一:

class Subsquare {
public:
    int row, col, size;
    Subsquare(int r, int c, int sz): row(r), col(c), size(sz) {}
    void print() {
        cout << "(" << row << ", " << col << ", " << size << ")" << endl;
    }
};

bool is_square(vector<vector<int>> &matrix, int row, int col, int size) {
    for (int j = 0; j < size; ++j) {
        if (matrix[row][col + j] == 1) return false;
        if (matrix[row + size - 1][col + j] == 1) return false;
    }
    for (int i = 1; i < size - 1; ++i) {
        if (matrix[row + i][col] == 1) return false;
        if (matrix[row + i][col + size - 1] == 1) return false;
    }
    return true;
}

Subsquare* find_square_with_size(vector<vector<int>> &matrix, int squareSize) {
    int cnt = matrix.size() - squareSize + 1;
    for (int row = 0; row < cnt; ++row) {
        for (int col = 0; col < cnt; ++col) {
            if (is_square(matrix, row, col, squareSize)) {
                return new Subsquare(row, col, squareSize);
            }
        }
    }
    return NULL;
}

Subsquare* find_square(vector<vector<int>> &matrix) {
    for (int i = matrix.size(); i >= 1; --i) {
        Subsquare *square = find_square_with_size(matrix, i);
        if (square) return square;
    }
    return NULL;
}

解法二:

class Subsquare {
public:
    int row, col, size;
    Subsquare(int r, int c, int sz): row(r), col(c), size(sz) {}
    void print() {
        cout << "(" << row << ", " << col << ", " << size << ")" << endl;
    }
};

class SquareCell {
public:
    int zerosRight = 0, zerosBelow = 0;
    SquareCell(int right, int below): zerosRight(right), zerosBelow(below){}
    void setZerosRight(int right) {
        zerosRight = right;
    }
    void setZerosBelow(int below) {
        zerosBelow = below;
    }
};

bool is_square(vector<vector<SquareCell*>> &matrix, int row, int col, int size) {
    SquareCell *topLeft = matrix[row][col];
    SquareCell *topRight = matrix[row][col + size - 1];
    SquareCell *bottomRight = matrix[row + size - 1][col];
    if (topLeft->zerosRight < size) return false;
    if (topLeft->zerosBelow < size) return false;
    if (topRight->zerosBelow < size) return false;
    if (bottomRight->zerosRight < size) return false;
    return true;
}

vector<vector<SquareCell*>> process_square(vector<vector<int>> &matrix) {
    vector<vector<SquareCell*>> res(matrix.size(), vector<SquareCell*>(matrix.size()));
    for (int r = matrix.size() - 1; r >= 0; --r) {
        for (int c = matrix.size() - 1; c >= 0; --c) {
            int rightZeros = 0, belowZeros = 0;
            if (matrix[r][c] == 0) {
                ++rightZeros;
                ++belowZeros;
                if (c + 1 < matrix.size()) {
                    SquareCell *pre = res[r][c + 1];
                    rightZeros += pre->zerosRight;
                }
                if (r + 1 < matrix.size()) {
                    SquareCell *pre = res[r + 1][c];
                    belowZeros += pre->zerosBelow;
                }
            }
            res[r][c] = new SquareCell(rightZeros, belowZeros);
        }
    }
    return res;
}

Subsquare* find_square_with_size(vector<vector<SquareCell*>> &processed, int square_size) {
    int cnt = processed.size() - square_size + 1;
    for (int row = 0; row < cnt; ++row) {
        for (int col = 0; col < cnt; ++col) {
            if (is_square(processed, row, col, square_size)) {
                return new Subsquare(row, col, square_size);
            }
        }
    }
    return NULL;
}

Subsquare* find_square(vector<vector<int>> &matrix) {
    vector<vector<SquareCell*>> processed = process_square(matrix);
    // cout << "here" << endl;
    for (int i = matrix.size(); i >= 1; --i) {
        Subsquare *square = find_square_with_size(processed, i);
        if (square) return square;
    }
    return NULL;
}

本文转自博客园Grandyang的博客,原文链接: 最大子方形[CareerCup] 18.11 Maximum Subsquare,如需转载请自行联系原博主。

相关文章
|
19小时前
|
云安全 人工智能 自然语言处理
|
5天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
310 116
|
8天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
550 51
Meta SAM3开源:让图像分割,听懂你的话
|
20天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
4天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
|
3天前
|
弹性计算 人工智能 Cloud Native
阿里云无门槛和有门槛优惠券解析:学生券,满减券,补贴券等优惠券领取与使用介绍
为了回馈用户与助力更多用户节省上云成本,阿里云会经常推出各种优惠券相关的活动,包括无门槛优惠券和有门槛优惠券。本文将详细介绍阿里云无门槛优惠券的领取与使用方式,同时也会概述几种常见的有门槛优惠券,帮助用户更好地利用这些优惠,降低云服务的成本。
263 132
|
8天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
385 29
|
14天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
703 224