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. loj 1038(dp求期望)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25915 题意:求一个数不断地除以他的因子,直到变成1的时候 除的次 ...

  2. MFC中afx_msg是什么,afx_msg void function()是什么意思

    应用程序框架产生的消息映射函数例如:afx_msg void OnBnClickedButton1(); 其中 afx_msg为消息标志,它向系统声明:有消息映射到函数实现体:而在map宏定义中,就有 ...

  3. Android:res之shape制作圆角、虚线、渐变

    xml控件配置属性 android:background="@drawable/shape" 标签 corners ----------圆角gradient ----------渐 ...

  4. hdu 2669 Romantic

    Romantic Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  5. Android性能优化之布局优化

    最新最准确内容建议直接访问原文:Android性能优化之布局优化 本文为Android性能优化的第二篇——布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不 ...

  6. 分享Kali Linux 2016.2第48周镜像文件

    分享Kali Linux 2016.2第48周镜像文件Kali Linux官方于11月27日发布Kali Linux 2016.2的第48周镜像.这次延续以往规律,仍然是11个镜像文件.默认的Gnom ...

  7. wpf中dropdownButton控件下拉居中。。。

    设置模版中popup控件的HorizontalOffset属性来控制居中. 还是对popup控件不熟,折腾了一会.

  8. sed 字符串替换

    1. sed替换的基本语法为: sed 's/原字符串/替换字符串/' 单引号里面,s表示替换,三根斜线中间是替换的样式,特殊字符需要使用反斜线”\”进行转义. 2. 单引号” ‘ ’”是没有办法用反 ...

  9. MFC 定义和调用全局变量的一种方法

    在CTestApp.h中声明一个int x;然后调用的方式如下: CTestApp *app = (CTestApp *)AfxGetApp(); //生成指向应用程序类的指针,Test处改为对应的项 ...

  10. 由addOneMember引发的思考

    addOneMember是一个方法,这个方法在两处地方重复了. 所以在修改页面的时候,发现修改了一处,如果是新手,肯定不会注意到另外一处有问题,他如果没有看清楚这个类到底整体怎样,那么他会犯的错误是就 ...