Palindrome Sub-Array
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 173 Accepted Submission(s): 80
Problem Description 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.
Input 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.
Output For each test case, output P only, the size of the maximum sub-array that you need to find.
Sample Input 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
Sample Output 4
Source
2013 Multi-University Training Contest 2
Recommend zhuyuanchen520
很简单的题目
那个时候想复杂了
只要暴力过去就行了。
枚举中心点,然后扩展。
偶数和奇数行分开算
#include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #include <set> #include <map> #include <vector> #include <queue> #include <string> #include <math.h> using namespace std; const int MAXN = 330; int a[MAXN][MAXN]; int n,m; int calc(int x,int y,int tt)//tt=0奇数,tt=1偶数 { int ans; int x1,y1,x2,y2;//两个角坐标 if(tt == 0) { ans = 1; x1 = x; y1 = y; x2 = x; y2 = y; } else { if(x+1>=n || y+1>=m)return 0; ans = 2; x1 = x;y1 = y; x2 = x+1;y2 = y+1; if(a[x1][y1]!=a[x2][y1]||a[x1][y1]!=a[x2][y1]) return 0; if(a[x2][y2]!=a[x2][y1]||a[x2][y2]!=a[x2][y1]) return 0; } while(1) { x1--;y1--; x2++;y2++; if(x1 < 0 || y1 < 0 || x2 >= n || y2 >= m) return ans; for(int i = x1;i <= x2;i++) if(a[i][y1]!=a[x2-i+x1][y1]) return ans; for(int i = x1;i <= x2;i++) if(a[i][y2]!=a[x2-i+x1][y2]) return ans; for(int i = y1;i <= y2;i++) if(a[x1][i]!=a[x1][y2-i+y1]) return ans; for(int i = y1;i <= y2;i++) if(a[x2][i]!=a[x2][y2-i+y1]) return ans; for(int i = x1;i <= x2;i++) if(a[i][y1]!=a[i][y2]) return ans; for(int i = y1;i <= y2;i++) if(a[x1][i]!=a[x2][i]) return ans; ans += 2; } return ans; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i = 0; i < n;i++) for(int j = 0;j < m;j++) scanf("%d",&a[i][j]); int ans = 0; for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) { ans = max(ans,calc(i,j,0)); ans = max(ans,calc(i,j,1)); } printf("%d\n",ans); } return 0; return 0; }