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 = ; j < size; ++j) {
if (matrix[row][col + j] == ) return false;
if (matrix[row + size - ][col + j] == ) return false;
}
for (int i = ; i < size - ; ++i) {
if (matrix[row + i][col] == ) return false;
if (matrix[row + i][col + size - ] == ) return false;
}
return true;
} Subsquare* find_square_with_size(vector<vector<int>> &matrix, int squareSize) {
int cnt = matrix.size() - squareSize + ;
for (int row = ; row < cnt; ++row) {
for (int col = ; 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 >= ; --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 = , zerosBelow = ;
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 - ];
SquareCell *bottomRight = matrix[row + size - ][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() - ; r >= ; --r) {
for (int c = matrix.size() - ; c >= ; --c) {
int rightZeros = , belowZeros = ;
if (matrix[r][c] == ) {
++rightZeros;
++belowZeros;
if (c + < matrix.size()) {
SquareCell *pre = res[r][c + ];
rightZeros += pre->zerosRight;
}
if (r + < matrix.size()) {
SquareCell *pre = res[r + ][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 + ;
for (int row = ; row < cnt; ++row) {
for (int col = ; 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 >= ; --i) {
Subsquare *square = find_square_with_size(processed, i);
if (square) return square;
}
return NULL;
}

CareerCup All in One 题目汇总

[CareerCup] 18.11 Maximum Subsquare 最大子方形的更多相关文章

  1. 日本IT行业劳动力缺口达22万 在日中国留学生迎来就业好时机 2017/07/18 11:25:09

    作者:倪亚敏 来源:日本新华侨报 发布时间:2017/07/18 11:25:09     据日本政府提供的数据,日本2018年应届毕业生的“求人倍率”已经达到了1.78倍.换言之,就是100名大学生 ...

  2. [18/11/11] java标识符及变量

    一.标识符规范 1.必须以字母.下划线 .美元符号开头. 即数字不能作为开头,其它位随便 2.不可以是java关键字(即保留字),  如static .class.new 等 .    注:int 年 ...

  3. [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵

    18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with t ...

  4. [CareerCup] 18.4 Count Number of Two 统计数字2的个数

    18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如 ...

  5. highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏

    /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...

  6. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  8. [CareerCup] 18.10 Word Transform 单词转换

    18.10 Given two words of equal length that are in a dictionary, write a method to transform one word ...

  9. [CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数

    18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the ...

随机推荐

  1. leetcode4568

    date: 2015-09-13 16:32:49 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of ...

  2. document对象(二)

    五.相关元素操作: var a = document.getElementById("id");                找到a: var b = a.nextSibling ...

  3. RPC和Socket,RMI和RPC之间的关系

    远程通信机制RPC与RMI的关系 http://blog.csdn.net/zolalad/article/details/25161133       1.RPC RPC(Remote Proced ...

  4. Laravel系列2入门使用

    最好的教程是官方文档! homestead安装好,就可以使用了. 安装Laravel composer create-project --prefer-dist laravel/laravel blo ...

  5. ReportViewer报表

    个人感觉ReportViewer>DataGridView>listView 打开一个空的winform窗体程序,工具栏报表拖入 ReportViewer 在空的Form1中 在同一命名空 ...

  6. 创建com服务器

    Delphi Com深入编程 第二章:

  7. VS链接过程中与MSVCRT.lib冲突

    vs代码生成有/MT,/MTd,/Md,/MDd四个编译选项,分别代表多线程.多线程调试.多线程DLL.多线程调试DLL. 编译时引用的lib分别为libcmt.li.libcmtd.lib.msvc ...

  8. Image Transformation in WPF输入日志标题

    Image transformation is a process of rotating and scaling images. Rotating Images There are two ways ...

  9. OpenCV 第二课 认识图像的存储结构

    OpenCV 第二课 认识图像的存储结构 Mat Mat 类包含两部分,矩阵头和矩阵体.矩阵头包含矩阵的大小,存储方式和矩阵体存储空间的指针.因此,Mat中矩阵头的大小是固定的,矩阵体大小是不定的. ...

  10. display:none 与 opacity:0

    display:none隐藏消失: opacity:0 只是透明: