Skip to content

Instantly share code, notes, and snippets.

@myegor
Created February 3, 2019 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myegor/2927331724e50613bfbe95e8d176f8c3 to your computer and use it in GitHub Desktop.
Save myegor/2927331724e50613bfbe95e8d176f8c3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Вычисляем максимальный квадрат с правым нижним
// углом в точке (i,j) на основе ужн вычесленного для точки
// (i-1,j-1) . Для этого нам также надо знать сколько
// свободных ячеек левее и выше точки (i,j)
std::pair<int, int> Solve(const vector<string>& a)
{
vector<vector<int>> r(a.size(),vector<int>(a[0].size(), -1));
vector<int> v(a[0].size(), -1);
int m = 0;
int count = 0;
for (int i = 0; i < a.size(); ++i) {
int h = -1;
for (int j = 0; j < a[0].size(); ++j) {
if (a[i][j] == '1' || j == 0 || i == 0) {
auto c = a[i][j] != '1';
r[i][j] = c;
h = c ? h : j;
v[j] = c ? v[j] : i;
continue;
}
auto _a = r[i-1][j-1]+1;
auto _b = i - v[j];
auto _c = j - h;
r[i][j] = min(_a, min(_b, _c));
if (r[i][j] == m) {
++count;
}
if (m < r[i][j]) {
m = r[i][j];
count = 1;
}
}
}
return make_pair(m, count);
}
int main() {
{
auto r = Solve(
{
"100001",
"010000",
"100000",
"001000",
"000010",
"001000"
});
cout << r.first << " " << r.second << endl;
}
{
auto r = Solve(
{
"111111",
"111111",
"111111",
"111111",
"111111",
"111111"
});
cout << r.first << " " << r.second << endl;
}
{
auto r = Solve(
{
"000000",
"000000",
"000000",
"000000",
"000000",
"000000"
});
cout << r.first << " " << r.second << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment