HDU-4618 Palindrome Sub-Array 暴力枚举

时间:2024-04-27 09:34:46

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4618

  直接暴力枚举中心点,在中间如果求不出最大值直接跳过优化下。。。

 //STATUS:C++_AC_31MS_800KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=,M=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int w[N][N];
int T,n,m,ans; int slove(int x,int y)
{
int i,j,c,ok,ret=;
for(c=ok=;x-c>=&&x+c<n && y-c>=&&y+c<m;c++){
for(i=;i<=c;i++){
if(w[x-c+i][y-c]!=w[x-c+i][y+c]
|| w[x-c][y-c+i]!=w[x+c][y-c+i]
|| w[x+i][y-c]!=w[x-i][y-c]
|| w[x+i][y+c]!=w[x-i][y+c]
|| w[x-c][y-i]!=w[x-c][y+i]
|| w[x+c][y-i]!=w[x+c][y+i]){ok=;break;}
}
if(!ok)break;
}
ret=(c-)<<|;
for(c=ok=;x-c+>=&&x+c<n && y-c+>=&&y+c<m;c++){
for(i=;i<=c;i++){
if(w[x-c+i+][y-c+]!=w[x-c+i+][y+c]
|| w[x-c+][y-c+i+]!=w[x+c][y-c+i+]
|| w[x+i][y-c+]!=w[x-i+][y-c+]
|| w[x+i][y+c]!=w[x-i+][y+c]
|| w[x-c+][y-i+]!=w[x-c+][y+i]
|| w[x+c][y-i+]!=w[x+c][y+i]){ok=;break;}
}
if(!ok)break;
}
return Max(ret,(c-)<<);
} int main()
{
// freopen("in.txt","r",stdin);
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=;i<n;i++){
for(j=;j<m;j++){
scanf("%d",&w[i][j]);
}
} ans=;
for(i=;i<n;i++){
if( ((i+)<<)<=ans || (((n-i-)<<)|)<=ans)continue;
for(j=;j<m;j++){
if( ((j+)<<)<=ans || (((m-j-)<<)|)<=ans)continue;
ans=Max(ans,slove(i,j));
}
} printf("%d\n",ans);
}
return ;
}