Tuesday, 7 May 2013
UVa 836 - Largest Submatrix Solution
#include<iostream>
#include<cstdio>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
#include<deque>
#include<iterator>
#include<assert.h>
#include<bitset>
#include<climits>
#include<ctime>
#include<complex>
using namespace std;
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long
#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define KS printf("Case %d: ",kk++)
#define MOD 1000000007
#define MX 100010
int row[110][110],mat[110][110],n,m;
int func(int start, int end)
{
int i,j,curnt=0, mx=-1270000;
for(i=1;i<=n;i++)
{
j=row[i][end]-row[i][start-1];
curnt+=j;
if(curnt>mx) mx=curnt;
if(curnt<0) curnt=0;
}
return mx;
}
int main()
{
int i,j,temp,tc;
string s[30];
cin>>tc;
while(tc--)
{
cin>>s[0];
n=s[0].size();
for(i=0;i<=n;i++)
row[i][0]=0;
for(int i=0;i<n;i++)
if(s[0][i]=='0')
mat[1][i+1]=-1e5;
else mat[1][i+1]=1;
for(int i=1;i<n;i++)
{
cin>>s[i];
for(int j=0;j<n;j++)
if(s[i][j]=='0')
mat[i+1][j+1]=-1e5;
else mat[i+1][j+1]=1;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
row[i][j]=row[i][j-1]+mat[i][j];
temp=0;
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
temp=max(temp,func(i,j));
cout<<temp<<endl;
if(tc) cout<<endl;
}
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment