動態規劃----最大矩陣
阿新 • • 發佈:2019-02-07
#include <iostream>
#include <vector>
using namespace std;
int Solution(vector<vector<char>>& matrix)
{
int m = matrix.size();
int n = matrix[0].size();
vector<int> L(n, 0),R(n,n),H(n,0);
int ret = 0;
int left = 0, right = n;
for (int i = 0; i < m; i++)
{
left = 0;
right = n;
for (int j = 0; j < n; j++)
{
if (matrix[i][j] == '1')
{
L[j] = left>L[j]? left:L[j];
H[j]++;
}
else
{
left = j+1;
H[j] = 0;
R[j] = n;
L[j] = 0;
}
}
for (int j = n - 1; j >= 0; j--)
{
if (matrix[i][j] == '1')
{
R[j] = right < R[j] ? right : R[j];
ret = ret >((R[j] - L[j])*H[j]) ? ret : ((R[j] - L[j])*H[j]);
}
else
{
right = j;
}
}
}
return ret;
}
int main()
{
vector<vector<char>> m = {
{'0','1','1','1'},
{ '0','1','1','1' },
{ '1','1','1','1' }
};
cout << m[1][1] << endl;
cout << Solution(m) << endl;
}
#include <vector>
using namespace std;
int Solution(vector<vector<char>>& matrix)
{
int m = matrix.size();
int n = matrix[0].size();
vector<int> L(n, 0),R(n,n),H(n,0);
int ret = 0;
int left = 0, right = n;
for (int i = 0; i < m; i++)
{
left = 0;
right = n;
for (int j = 0; j < n; j++)
{
if (matrix[i][j] == '1')
{
L[j] = left>L[j]? left:L[j];
H[j]++;
}
else
{
left = j+1;
H[j] = 0;
R[j] = n;
L[j] = 0;
}
}
for (int j = n - 1; j >= 0; j--)
{
if (matrix[i][j] == '1')
{
R[j] = right < R[j] ? right : R[j];
ret = ret >((R[j] - L[j])*H[j]) ? ret : ((R[j] - L[j])*H[j]);
}
else
{
right = j;
}
}
}
return ret;
}
int main()
{
vector<vector<char>> m = {
{'0','1','1','1'},
{ '0','1','1','1' },
{ '1','1','1','1' }
};
cout << m[1][1] << endl;
cout << Solution(m) << endl;
}