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. 笔记本电脑关闭小键盘(即打字按P出现星号键)

    开关方法:Fn + NumLk (联想电脑的NumLk 一般为F8,其他电脑自己在键盘找找罗)

  2. C++中单例模式

    //C++单例模式:指一个类只生成一个对象 #include <iostream> using namespace std; class A{ public: static A* getA ...

  3. mybatis 中#和$的区别

    #{…}是一个参数标记,将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是1,那么解析成sql时的值为order by " ...

  4. Activity有四种加载模式(转)

    Activity有四种加载模式: standard singleTop singleTask singleInstance 在多Activity开发中,有可能是自己应用之间的Activity跳转,或者 ...

  5. loj 1426(dfs + bfs)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1426 思路:首先我们预处理出每一个"*"在某一方向上最终能到达的位 ...

  6. Linux编程(3) MakeFile

    1. 在Linux中,make工具可以维护程序模块关系和生成可执行程序.它可根据程序模块的修改情况重新编译链接生成中间代码或最终的可执行程序.执行make命令,需要一个名为Makefile的文本文件, ...

  7. JSON详解以及可以把javabean转换成json串的json-lib应用

    JSON 1. json是什么 它是js提供的一种数据交换格式! 2. json的语法 {}:是对象! 属性名必须使用双引号括起来!单引不行!!! 属性值:null,数值,字符串,数组:使用[]括起来 ...

  8. Servlet请求头response应用简单案例

    Servlet请求头response应用简单案例:访问AServlet重定向到BServlet,5秒后跳到CServlet,并显示图片: AServlet package cn.yzu; import ...

  9. lr中switch的应用

    Action() { char *time; int i,j,length; time=lr_eval_string("{testtime}"); lr_error_message ...

  10. Loadrunner中参数化实战(1)-Sequential+Each iteration

    参数化数据30条: 脚本如下,演示登录,投资,退出操作是,打印手机号: 首先验证Vugen中迭代: Sequential+Each iteration 设置迭代4次Action 结果如下: