poj 4618 暴力
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4618
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <queue> 7 #include <vector> 8 #define maxn 305 9 using namespace std; 10 11 vector<int> G[maxn*maxn]; 12 int a[maxn][maxn]; 13 int maxp; 14 int N,M; 15 16 bool find(int u,int v){ 17 int d = G[u].size(); 18 for(int i=0;i<d;i++){ 19 if(G[u][i] == v) return true; 20 } 21 return false; 22 } 23 bool JudgeRowPalindrome(int x1,int y1,int x2,int y2){ 24 if(y1 >= y2) return true; 25 if(a[x1][y1] == a[x2][y2]){ 26 if(find((x1)*N+y1+1,(x2)*N+y2-1)) return true; 27 if(JudgeRowPalindrome(x1,y1+1,x2,y2-1)){ 28 G[x1*N+y1+1].push_back(x2*N+y2-1); 29 return true; 30 } 31 } 32 else return false; 33 } 34 bool JudgeColPalindrome(int x1,int y1,int x2,int y2){ 35 if(x1 >= x2) return true; 36 if(a[x1][y1] == a[x2][y2]){ 37 if(find((x1+1)*N+y1,(x2-1)*N+y2)) return true; 38 39 if(JudgeColPalindrome(x1+1,y1,x2-1,y2)){ 40 G[(x1+1)*N+y1].push_back((x2-1)*N+y2); 41 return true; 42 } 43 } 44 else return false; 45 46 } 47 bool Judge(int x,int y,int len){ 48 for(int i=x;i<x+len;i++) 49 if(!find(N*i+y,N*i+y+len-1)) return false; 50 for(int i=y;i<y+len;i++) 51 if(!find(N*x+i,N*(x+len-1)+i)) return false; 52 return true; 53 } 54 int main() 55 { 56 // if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} 57 58 int T; 59 cin>>T; 60 while(T--){ 61 scanf("%d%d",&N,&M); 62 for(int i=0;i<N;i++) 63 for(int j=0;j<M;j++){ 64 scanf("%d",&a[i][j]); 65 } 66 for(int i=1;i<=N*M + N+M;i++) G[i].clear(); 67 68 for(int row=0;row<N;row++) 69 for(int i=0;i<M;i++) 70 for(int j=i+1;j<M;j++){ 71 if(find(N*row+i,N*row+j) || a[row][i] != a[row][j]) continue; 72 bool flag = JudgeRowPalindrome(row,i,row,j); 73 if(flag) G[N*row+i].push_back(N*row+j); 74 } 75 for(int col=0;col<M;col++) 76 for(int i=0;i<N;i++) 77 for(int j=i+1;j<N;j++){ 78 if(find(N*i+col,N*j+col) || a[i][col] != a[j][col]) continue; 79 bool flag = JudgeColPalindrome(i,col,j,col); 80 if(flag==true) G[N*i+col].push_back(N*j+col); 81 } 82 maxp = 1; 83 for(int i=0;i<N;i++) 84 for(int j=0;j<M;j++){ 85 if(N-i <= maxp || M-j <= maxp) continue; 86 if(N-1-i >= M-1-j){ 87 for(int k=M-1;k>=j+maxp;k--){ 88 if(find(N*i+j,N*i+k)){ 89 int p = k-j+1; 90 if(Judge(i,j,p)){ 91 maxp = p; 92 } 93 } 94 } 95 } 96 else{ 97 for(int k=N-1;k>=i+maxp;k--){ 98 if(find(N*i+j,N*k+j)){ 99 int p = k-i+1; 100 if(Judge(i,j,p)){ 101 maxp = p; 102 } 103 } 104 } 105 } 106 } 107 printf("%d\n",maxp); 108 } 109 }