UVA--836 Largest Submatrix
2014-11-22 12:01
375 查看
Let A be an N×N matrix of zeros and ones. A submatrix S of A is any group
of contiguous entries that forms a square or a rectangle.
Write a program that determines the number of elements of the largest submatrix of ones in A. Largest here is measured by area.
consecutive inputs.
The matrix is given line by line. Each line consists of 0's and 1's. The order of the matrix is also the number of lines input and 1
N
25.
The output is the number of elements of the largest submatrix found.
憋了一天多的题 终于过了 代码如下 :
看到一个更简单的实现 比我写的要简单很多 一针见血啊 我还需要学习 改进
of contiguous entries that forms a square or a rectangle.
Write a program that determines the number of elements of the largest submatrix of ones in A. Largest here is measured by area.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between twoconsecutive inputs.
The matrix is given line by line. Each line consists of 0's and 1's. The order of the matrix is also the number of lines input and 1
N
25.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.The output is the number of elements of the largest submatrix found.
Sample Input
1 10111000 00010100 00111000 00111010 00111111 01011110 01011110 00011110
Sample Output
16
憋了一天多的题 终于过了 代码如下 :
<span style="font-size:14px;">#include<stdio.h> #include<string.h> int a[30][30]; int max1d(int a[],int l){ int sum,max_sum=-1e9; int flog[30]; for(int i=0;i<l;i++){ memset(flog,0,sizeof(flog)); for(int j=i+1;j<l;j++){ if(a[j]!=a[i]||flog[j-1]==1) flog[j]=1; if(flog[j]==0) sum=a[i]*(j-i+1); else{ if(a[j]>a[i]) sum=a[j]; else sum=a[i]; } if(sum>max_sum) max_sum=sum; } } return max_sum; } int main(){ int t,cnt=0; scanf("%d",&t); //getchar(); while(t--){ cnt++; char str[30]; int s[30]; int flog[30]; int max_sum=-1e9; getchar(); scanf("%s",str); int l=strlen(str); if(l==1){ if(cnt==1) printf("%s\n",str); else printf("\n%s\n",str); } else{ for(int i=0;i<l;i++) a[0][i]=str[i]-'0'; for(int i=1;i<l;i++){ scanf("%s",str); for(int j=0;j<l;j++) a[i][j]=str[j]-'0'; } for(int i=0;i<l;i++){ memset(s,0,sizeof(s)); memset(flog,0,sizeof(flog)); for(int k=0;k<l;k++){ s[k]=a[i][k]; flog[k]=a[i][k]; //printf("%d ",s[k]); } //printf("\n"); for(int j=i+1;j<l;j++){ for(int k=0;k<l;k++){ if(a[j][k]==1&&flog[k]==1) s[k]+=a[j][k]; else flog[k]=0; } int sum=max1d(s,l); /*for(int k=0;k<l;k++) printf("%d ",s[k]); printf("\n");*/ if(sum>max_sum) max_sum=sum; //printf("%d %d\n",sum,max_sum); } } if(cnt==1) printf("%d\n",max_sum); else printf("\n%d\n",max_sum); } } return 0; } </span>
看到一个更简单的实现 比我写的要简单很多 一针见血啊 我还需要学习 改进
<span style="font-size:14px;">#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=30; int n; int max1(int* a){ //printf("8 ");for(int i=0;i<n;i++)printf("%d ",a[i]); puts(""); int ans=a[0],sum=a[0]; for(int i=1;i<n;i++){ if(a[i]==a[i-1]) sum+=a[i]; else sum=a[i]; ans=max(ans,sum); } //printf("15 ans=%d\n",ans); return ans; } int max2(char a[][MAXN]){ //for(int i=0;i<n;i++) {for(int j=0;j<n;j++) printf("%c",a[i][j]);puts("");} int ans=0,sum[MAXN]; for(int i=0;i<n;i++){ memset(sum,0,sizeof(sum)); for(int j=i;j<n;j++){ for(int k=0;k<n;k++){ if(a[j][k]=='1') sum[k]++; else sum[k]=0; } //printf("30");for(int i=0;i<n;i++)printf(" %d",sum[i]); puts(""); ans=max(ans,max1(sum)); } } return ans; } int main(){ char a[MAXN][MAXN]; int i,t; while(~scanf("%d",&t)){ while(t--){ scanf("%s",a[0]); n=strlen(a[0]); for(i=1;i<n;i++) scanf("%s",a[i]); //for(i=0;i<n;i++) printf("%s\n",a[i]); printf("%d\n",max2(a)); if(t) puts(""); } } } </span>
相关文章推荐
- UVa 836 - Largest Submatrix
- UVa 836 - Largest Submatrix
- UVa 836 - Largest Submatrix ( 最大01矩阵)
- UVA 836 Largest Submatrix(01矩阵求和)
- UVa 836 - Largest Submatrix
- uva 836 Largest Submatrix
- UVa:836 Largest Submatrix
- poj 3494 Largest Submatrix of All 1’s(单调栈)
- HDU 2870 Largest Submatrix (单调栈)
- HDU-2870 Largest Submatrix (线性dp 最大01矩阵)(2009 Multi-University Training Contest 7 )
- HDU 2870 Largest Submatrix
- HDU 2870 Largest Submatrix (找最大子矩阵)
- HDU--2870--Largest Submatrix
- poj 3494 Largest Submatrix of All 1’s
- hdu 2870 Largest Submatrix (dp)
- 【DP+预处理|最长连续子序列】HDU-2870 Largest Submatrix
- hdu 2870 Largest Submatrix
- [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵
- HDU 2870 Largest Submatrix (单调栈)
- Largest Submatrix(动态规划)