Description
The square of an integer.
A rectangle table of a * B (a, B <10). Each grid contains a number (0-9 ).
Considering such a sequence, it is composed of numbers in some grids in this table, it is required that the numbers in the sequence are in the table and the values of the rows and columns must form an equal difference sequence (for details, see the description of the sample data ).
Find a sequence from all possible sequences that constitute the maximum number of possible sequences.
-
Input
-
The first line is an integer T, indicating that there are T groups of test data. For each group of test data: For the first row, two integers A and B represent the rows and columns of the rectangular table. For each row of a, a string of B Length indicates the numbers in the corresponding grid.
-
Output
-
Output one row for each group of test data: the maximum number of possible partitions. If no partition number exists, output-1.
-
Sample Input
-
22 31234566 7379117812832524103617823349487255722937261
-
Sample output
-
64320356
-
Prompt
-
Note: In the first example, both 6 and 4 are located in the second row to form an equal-difference series; they are located in columns 3rd and 1 respectively to form an equal-difference series, at the same time, 8*8 = 64 indicates the maximum number of possible records. In the second example, the row values of 3203356 are 1, 2, 3, 4, 5, and 6, respectively, and the column values are 1, 2, 3, 4, 5, and 6, respectively. Form an equal difference series, respectively. At the same time, 566*566 = 3203356 is the maximum number of possible records.
#include<iostream>#include<math.h>#include <cstdio>using namespace std;void p(int num[],char ma[][20],int row,int col,int i,int j,int k){int nu[20];num[0]=0;num[1]=0;for(int t=0;t<k;t++){num[0]+=(ma[row+t*i][col+t*j]-48)*pow(10,(double)k-1-t);num[1]+=(ma[row+t*i][col+t*j]-48)*pow(10,(double)t);}if((int)(sqrt((double)num[0])*(int)sqrt((double)num[0]))!=num[0])num[0]=-2;if(((int)sqrt((double)num[1])*(int)sqrt((double)num[1]))!=num[1])num[1]=-2;}int main(){int tn,m,n;cin>>tn;for(int t=1;t<=tn;t++){scanf("%d%d",&m,&n);char mx[20][20];for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)cin>>mx[i][j];int sum=-1;int max=m;if(n>max) max=n;for(int k=1;k<=max;k++){for(int i=(-m+1);i<m;i++){for(int j=-n+1;j<n;j++){for(int row=1;row<=m;row++)for(int col=1;col<=n;col++){if((row+(k-1)*i)<=m&&(col+(k-1)*j)<=n&&1<=(row+(k-1)*i)&&1<=(col+(k-1)*j)){int num[2];p(num,mx,row,col,i,j,k);if(num[0]>sum)sum=num[0];if(num[1]>sum)sum=num[1];}}}}}printf("%d\n",sum);}return 0;}