从计算矩形的面积到计算1的矩阵
计算矩形的最大面积
#include <stdio.h>
#include <stack>
using namespace std;
/**
单调递增栈,就最大矩形的面积
*/
struct Node
{
long long height;//一个高度值
int startIdx; //这个高度值的起始位置
Node(long long _height, int _idx):height(_height), startIdx (_idx) {}
};
long long gHeights[100000];
long long GetMaxArea(int nItem)
{
int i;
stack<Node> s;
long long height;
s.push(Node(-1, 0));//将最小高度加入堆栈,防止堆栈弹空
int currentPosition;
long long maxArea = 0;//记录最大面积
long long curArea;
for( i = 0; i <= nItem ; i++)
{
currentPosition = i + 1;//获得当前 位置
if( i == nItem)//这时候,我们认为到达最后,我们要弹空栈
{
height = 0;
}
else
{
height = gHeights[currentPosition-1];
}
Node t(height, currentPosition);//当前节点
while( s.top().height > height)
{
t = s.top();
s.pop();
curArea = (currentPosition - t.startIdx) * t.height;//按照某个高度的 开始和结束的位置,获得面积 核心!!!
if(curArea > maxArea)
{
maxArea = curArea;
}
}
s.push(Node(height, t.startIdx)); //!重点在这里,序号是上一个的序号
}
return maxArea;
}
int main()
{
int nItem;
while(scanf("%d", &nItem) != EOF && nItem)
{
int i;
for( i = 0; i < nItem; i++)
{
scanf("%lld", gHeights + i);
}
printf("%lld\n", GetMaxArea(nItem));
}
return 0;
}
计算矩阵中最大1的矩阵,输出1的数量
其中要到了上面的的原理,这里做的只是处理矩阵(分别把每一行1的最大数算出来),然后把每一行的最大面积算出来。最后取所有行的最大数
我用了好多vector<vector<int>>
不知道和2维数组哪个更好点
用图形表示一下清晰点:
1 0 0 1 1
1 1 1 0 0
0 0 1 1 1
处理完毕后的第一行(处理完的数据就好像是高度为2 0 0 1 1 的矩形,然后求最大面积)
2 0 0 1 1
处理完毕后的第二行
1 1 2 0 0
处理完毕后的第二行
0 0 1 1 1
#include <stdio.h>
#include <stack>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void matrix(vector<vector<int>> &v) //创建矩阵
{
// vector<int> v1{0,1,0,1,0,0};
// vector<int> v2{0,1,1,1,1,1};
// vector<int> v3{1,1,1,1,1,1};
// vector<int> v4{0,1,1,0,0,1};
//
//
// v.push_back(v1);
// v.push_back(v2);
// v.push_back(v3);
// v.push_back(v4);
vector<int> v1;
int row,col,x;
int m,n;
cout<<"分别输入矩阵的行数和列数:"<<endl;
cin>>row;
cin>>col;
cout<<"输入矩阵:"<<endl;
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
cin>>x;
v1.push_back(x);
}
v.push_back(v1);
v1.clear();
}
}
void GetEachLine(vector<vector<int>>& v, vector<int> &NewR, int i)
{
//先整理
for(int j=0; j<v[i].size() ; j++)
{
int n=0;
int x=i;
for(x; x<v.size(); x++)
{
if(v[x][j]==1)
{
n+=v[x][j];
}
else
break;
}
NewR.push_back(n);
}
}
struct Node
{
int height;//一个高度值
int startIdx; //这个高度值的起始位置
Node(int _height, int _idx):height(_height), startIdx(_idx)
{
}
};
vector<int> gHeights;
int GetMaxArea(int nItem)
{
int i;
stack<Node> s;
int height;
s.push(Node(-1, 0));//将最小高度加入堆栈,防止堆栈弹空
int currentPosition;
long long maxArea = 0;//记录最大面积
long long curArea;
for( i = 0; i <= nItem ; i++)
{
currentPosition = i + 1;//获得当前 位置
if( i == nItem)//这时候,我们认为到达最后,我们要弹空栈
{
height = 0;
}
else
{
height = gHeights.at(currentPosition-1);
}
Node t(height, currentPosition);//当前节点
while( s.top().height > height)
{
t = s.top();
s.pop();
curArea = (currentPosition - t.startIdx) * t.height;//按照某个高度的 开始和结束的位置,获得面积
if(curArea > maxArea)
{
maxArea = curArea;
}
}
s.push(Node(height, t.startIdx));
}
return maxArea;
}
int main()
{
vector<vector<int>> v;
vector<int> NewR;
vector<int> End;
matrix(v);
int i=0;
int nItem;
for(i; i<v.size(); i++)
{
GetEachLine(v,NewR,i);
cout<<"第一行:"<<endl;
for(auto &out:NewR)
{
cout<<out<<" ";
}
cout<<endl;
gHeights = NewR;
nItem=gHeights.size();
End.push_back(GetMaxArea(nItem));
NewR.clear();
}
auto biggest = max_element(End.begin(),End.end());
cout<<"最后结果:"<<*biggest<<endl;
}