题目大意:这题和10074几乎一样,只不过没有给出矩阵的边长。
解题策略:解法同UVA 10074, (传送门:http://blog.csdn.net/j_dark/article/details/8700390)
输入无比恶心,时间都费在这里了,大家注意!
/*
UVA 836 Largest Submatrix
AC by J.Dark
ON 2013/3/21
Time 0.008s
*/
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 110;
int matrix[maxn][maxn];
int matrix_temp[maxn];
int width, height;
inline void input(){
char str[maxn][maxn], tt[maxn];
int tempInt, flag=1, kk=0;
width = height = 0;
while(gets(tt) && tt[0])
{
for(height=0; tt[height]; height++) matrix[width][height] = tt[height] - '0';
width++;
}
}
int max_sum(int *b, int *matrix, int len_colu){
int C_sum = 0; //当前列最大子区间和
int temp = 0;
for(int i=0; i<height; i++) b[i] += matrix[i]; //当前第i-j行,长度为1~j-i+1的“条”的长度
//依次枚举i-j行间,长度从1~j-i+1的“条”
for(int i=0; i<height; i++){
if(b[i] < len_colu){ //出现“1”不连续情况则抛弃
temp = 0;
continue;
}
temp += b[i];
if(temp > C_sum) C_sum = temp;
}
return C_sum;
}
void solve(int count1){
int maxSum = 0, b[maxn];
int temp = 0;
for(int i=0; i<width; i++){
memset(b, 0, sizeof(b));
for(int j=i; j<width; j++){
//枚举第i行~第j行的 满足题意的子矩阵元素数,取最大
temp = max_sum(b, matrix[j], j-i+1);
maxSum = max(maxSum, temp);
}
}
if(count1 == 0) cout << maxSum << endl;
else cout << endl << maxSum << endl;
}
///
int main(){
int testCase;
while(cin >> testCase)
{
getchar();
getchar();
for(int i=0; i<testCase; i++){
input();
solve(i);
}
}
//system("pause");
return 0;
}