题目链接:https://ac.nowcoder.com/acm/contest/888/A
题目描述
Gromah and LZR entered the great tomb, the first thing they see is a matrix of size n×mn imes mn×m, and the elements in the matrix are all 0 or 1.
LZR finds a note board saying "An all-one matrix is defined as the matrix whose elements are all 1, you should determine the number of all-one submatrices of the given matrix that are not completely included by any other all-one submatrices".
Meanwhile, Gromah also finds a password lock, obviously the password should be the number mentioned in the note board!
输入描述:
The first line contains two positive integers n,m, denoting the size of given matrix.
Following n lines each contains a string with length m, whose elements are all 0 or 1, denoting the given matrix.
1≤n,m≤3000
输出描述:
Print a non-negative integer, denoting the answer.
示例1
说明
The 5 matrices are (1,2)−(1,4), (1,2)−(2,3), (1,2)−(3,2), (2,1)−(2,3), (3,4)−(3,4)
#include<iostream> #include<algorithm> using namespace std; #define maxn 3005 int a[maxn][maxn]; int h[maxn];//存该列往上有多少个1 int l[maxn],r[maxn]; int vis[maxn][maxn]; struct node{ int L,R; }hv[maxn][maxn]; int main() { ios::sync_with_stdio(false); int n,m; int ans=0; cin>>n>>m; char ch; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { cin>>ch; a[i][j]=ch=='0'?0:1; } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(a[i][j]==1) h[j]++; else h[j]=0; } h[0]=h[m+1]=-1; for(int j=1;j<=m;j++)//往左最远走到哪 { int k=j; while(h[j]<=h[k-1]) k=l[k-1]; l[j]=k; } for(int j=m;j>=1;j--)//往右最远走到哪 { int k=j; while(h[j]<=h[k+1]) k=r[k+1]; r[j]=k; } for(int j=1;j<=m;j++) { if(h[j]&&!vis[l[j]][r[j]]&&(hv[i-1][j].L!=l[j]||hv[i-1][j].R!=r[j])) { ans++; vis[l[j]][r[j]]=1; } if(h[j]) { hv[i][j].L=l[j]; hv[i][j].R=r[j]; } } for(int j=1;j<=m;j++) vis[l[j]][r[j]]=0; } cout<<ans<<endl; return 0; }