Largest Submatrix of All 1’s

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 9559 Accepted: 3413
Case Time Limit: 2000MS

Description

Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

Input

The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with n numbers. The input ends once EOF is met.

Output

For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.

Sample Input

2 2
0 0
0 0
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
Sample Output

0
4

题目大意:
给你一个n*m的矩阵也要你求最大的全1矩阵中1的个数。

一开始的思路是bfs把子矩阵里面的1全部清0并且记录1的个数。可惜交叉的1处理不来例如:
5 7
0 0 1 1 0 0 0
0 0 1 1 0 1 1
1 1 1 1 0 0 0
1 1 1 1 0 0 0
0 0 0 0 0 0 0
最大应该是8,但是输出12
代码:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

int n,m;
const int maxn=2000+10;
bool a[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct node{
	int x,y;
	node(int a,int b):x(a),y(b){}
};
int bfs(int x,int y){
	int s=1;
	queue<node>st;
	vis[x][y]=1;
	a[x][y]=0;
	st.push(node(x,y));
	while(!st.empty()){
		node now=st.front();
		st.pop();
		for(int i=0;i<4;i++){
			int fx=now.x+dir[i][0];
			int fy=now.y+dir[i][1];
			if(a[fx][fy]==1&&!vis[fx][fy]){
				vis[fx][fy]=1;
				s++;
				a[fx][fy]=0;
				st.push(node(fx,fy));
			}
		}
	}
	return s;
}
int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(a,false,sizeof(a));
		memset(vis,false,sizeof(vis));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				scanf("%d",&a[i][j]);
			}
		}
		int _max1=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][j]==1){
					_max1=max(_max1,bfs(i,j));
				}
			}
		}
		printf("%d\n",_max1);
	}
}

参考:https://blog.csdn.net/zuzhiang/article/details/78136417
后来看了这篇blog写出来的,大概思路就是对每行维护一个最大高然后就是单调栈求最大面积了。(01矩阵转换柱形图过于巧妙)
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=2000+20;
int w[maxn],h[maxn];
struct Stack{
	int top;
	int data[maxn];
	Stack(){top=-1;}
	bool empty(){return top==-1;}
};

int ans;
void push(Stack *stack,int x){
	if(stack->empty()||x>stack->data[stack->top]){
		stack->data[++stack->top]=x;
		w[stack->top]=1;
	}else{
		int wid=0;
		while(!stack->empty()&&x<=stack->data[stack->top]){
			wid+=w[stack->top];
			ans=max(ans,wid*stack->data[stack->top]);
			stack->top--;
		}
		stack->data[++stack->top]=x;
		w[stack->top]=wid+1;
	}
}
int main(){
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){
		ans=0;int x;
		memset(w,0,sizeof(w));
		memset(h,0,sizeof(h));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				scanf("%d",&x);
				if(x==1){
					h[j]=h[j]+1;
				}else if(x==0){
					h[j]=0;
				}
			}
			Stack *stack=new Stack();
			for(int k=0;k<n;k++){
				push(stack,h[k]);
			}
			push(stack,0);
		}
		printf("%d\n",ans);
	}
}