1551 search for shards

Source: Internet
Author: User
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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.