2015
09-17

# Palindrome Sub-Array

A palindrome sequence is a sequence which is as same as its reversed order. For example, 1 2 3 2 1 is a palindrome sequence, but 1 2 3 2 2 is not. Given a 2-D array of N rows and M columns, your task is to find a maximum sub-array of P rows and P columns, of which each row and each column is a palindrome sequence.

The first line of input contains only one integer, T, the number of test cases. Following T blocks, each block describe one test case.
There is two integers N, M (1<=N, M<=300) separated by one white space in the first line of each block, representing the size of the 2-D array.
Then N lines follow, each line contains M integers separated by white spaces, representing the elements of the 2-D array. All the elements in the 2-D array will be larger than 0 and no more than 31415926.

The first line of input contains only one integer, T, the number of test cases. Following T blocks, each block describe one test case.
There is two integers N, M (1<=N, M<=300) separated by one white space in the first line of each block, representing the size of the 2-D array.
Then N lines follow, each line contains M integers separated by white spaces, representing the elements of the 2-D array. All the elements in the 2-D array will be larger than 0 and no more than 31415926.

1
5 10
1 2 3 3 2 4 5 6 7 8
1 2 3 3 2 4 5 6 7 8
1 2 3 3 2 4 5 6 7 8
1 2 3 3 2 4 5 6 7 8
1 2 3 9 10 4 5 6 7 8

4

/*

各种暴力各种过。。

2013-07-26
*/

#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int N=306;

int n,m,ans,map[N][N];
int max(int a,int b){
return a>b?a:b;
}
int solve(int x1,int y1,int x2,int y2)
{
int i,l,a,b;
for(i=x1;i<=x2;i++)
{
a=y1;b=y2;
while(a<b && map[i][a]==map[i][b])	{a++;b--;}
if(a<b)	return 0;
}
for(l=y1;l<=y2;l++)
{
a=x1;b=x2;
while(a<b && map[a][l]==map[b][l])	{a++;b--;}
if(a<b)	return 0;
}
return 1;
}
int main()
{
int T;
int i,l,a,b,t;
cin>>T;
while(T--)
{
cin>>n>>m;
for(i=0;i<n;i++)
for(l=0;l<m;l++)
scanf("%d",&map[i][l]);
ans=1;
for(i=0;i<n;i++)
{
if(i+ans>=n)	break;
for(l=0;l<m;l++)
{
if(l+ans>=m)break;
t=(n-i-1)>(m-l-1)?(m-l-1):(n-i-1);
a=i+t;
b=l+t;
while(i+ans<=a)
{
if(solve(i,l,a,b))
{
ans=max(ans,a-i+1);
break;
}
a--;b--;
}
}
}
cout<<ans<<endl;
}
return 0;
}