The Hedgehog recently remembered one of his favorite childhood activities, — solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one.
Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces.
All puzzle pieces turn out to be of the same size X?×?Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A?×?B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers).
However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over).
Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size.
The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20.
Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters.
In the first line print the number of possible good puzzles (in other words, the number of pairs (X,?Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself.
In the second line print two numbers — the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly — by the length X.
2 4
ABDC
ABDC
3
2 1
2 6
ABCCBA
ABCCBA
1
2 6
111
枚举边长,然后枚举每个子方块,用hash检查
#include<iostream> #include<set> #include<cstdio> using namespace std; typedef long long ll; char board[30][30]; set<ll>used; int A,B,ans,x,y; bool check(int x,int y) { used.clear(); int t1=A/x,t2=B/y; for(int i=1;i<=t1;i++)for(int j=1;j<=t2;j++) { ll temp[5]; int cnt=0; for(int i=1;i<5;i++) temp[i]=0; ++cnt; for(int ii=1;ii<=x;ii++)for(int jj=1;jj<=y;jj++) { temp[cnt]*=171; temp[cnt]+=board[(i-1)*x+ii][(j-1)*y+jj]; } ++cnt; for(int ii=x;ii>=1;ii--)for(int jj=y;jj>=1;jj--) { temp[cnt]*=171; temp[cnt]+=board[(i-1)*x+ii][(j-1)*y+jj]; } ++cnt; if(x==y) { for(int jj=y;jj>=1;jj--)for(int ii=1;ii<=x;ii++) { temp[cnt]*=171; temp[cnt]+=board[(i-1)*x+ii][(j-1)*y+jj]; } ++cnt; for(int jj=x;jj>=1;jj--)for(int ii=1;ii<=y;ii++) { temp[cnt]*=171; temp[cnt]+=board[(i-1)*x+ii][(j-1)*y+jj]; } ++cnt; } for(int k=1;k<cnt;k++) if(used.count(temp[k])) return false; for(int k=1;k<cnt;k++) used.insert(temp[k]); } return true; } int main() { cin>>A>>B; for(int i=1;i<=A;i++) { for(int j=1;j<=B;j++) cin>>board[i][j]; } x=y=1<<10; for(int i=A;i>=1;i--)for(int j=B;j>=1;j--) { if(A%i==0&&B%j==0) if(check(i,j)) { ans++; // cout<<i<<" "<<j<<endl; if(i*j<x*y) {x=i;y=j;} else if(i*j==x*y) if(i<x){x=i;y=j;} } } cout<<ans<<endl; cout<<x<<" "<<y<<endl; return 0; }
原文:http://www.cnblogs.com/19992147orz/p/5979016.html