CF54B Cutting Jigsaw Puzzle题解

编程入门 行业动态 更新时间:2024-10-27 17:10:13

CF54B Cutting Jigsaw Puzzle<a href=https://www.elefans.com/category/jswz/34/1769599.html style=题解"/>

CF54B Cutting Jigsaw Puzzle题解

 蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!

Hello 大家好!本蒟蒻小学生来发题解啦,快来看看今天的题目!

=========================================================================

目录

题目描述

B. Cutting Jigsaw Puzzle

Input

Output

难度

题目意思

思路:

180度旋转:

 90度旋转:

270度旋转:

判断是否相同:

代码


题目描述


B. Cutting Jigsaw Puzzle

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.

Input

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.

Output

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.

难度:

题目意思


本题主要意思就是切成 一个个小块(小块的面积不变),使小块之间互不相等,而且旋转之后相同,也算相同!例:

AB    CA
CD    DB
这两个是相同的!

最后输出一共可以有多少种切法,使他们互不相等,然后输出切出的最小块  (这里要注意如果面积相等,则输出 a 小的那一个)比如说: 和 ,是要输出  !

思路:


这道题主要就是取块以及旋转判断

取块:这个很简单,只需双重for循环,不停的枚举中的 a 和 b,如果a或b不能被N或M整除,那么是不行的所以要continue!

旋转判断:这个就比较麻烦了!首先就是旋转,旋转要么是180度、90度或270度。但是长方形只用转180度,因为长方形转其他两个度数会改变形状,那么和其他长方形就不可能相等!

180度旋转:

A0,0 A0,1 A0,2        A2,2 A2,1 A2,0
A1,0 A1,1 A1,2        A1,2 A1,1 A1,0
A2,0 A2,1 A2,2        A0,2 A0,1 A0,0

我们可以发现Ax,y旋转180度之后 x是从a-1->0,y是从b-1->0,所以我们就for循环按前面的顺序读进一个string里:

for (int xx=i-1;xx>=0;xx--){//i就是afor (int yy=j-1;yy>=0;yy--){//j就是bpc+=pzl[x+xx][y+yy];}
}

 90度旋转:

A0,0 A0,1 A0,2        A2,0 A1,0 A0,0
A1,0 A1,1 A1,2        A2,1 A1,1 A0,1
A2,0 A2,1 A2,2        A2,2 A1,2 A0,2

我们可以发现Ax,y旋转90度之后 x是从a-1->0,y是从0->b-1,但是我们发现每一行y没在变化,所以y的for循环要在外面

for (int yy=0;yy<j;yy++){for (int xx=i-1;xx>=0;xx--){pc+=pzl[x+xx][y+yy];}
}

270度旋转:

A0,0 A0,1 A0,2        A0,2 A1,2 A2,2
A1,0 A1,1 A1,2        A0,1 A1,1 A2,1
A2,0 A2,1 A2,2        A0,0 A1,0 A2,0

我们可以发现Ax,y旋转270度之后 x是从0->a-1,y是从b-1->0,但是我们发现每一行y没在变化,所以y的for循环要在外面

for (int yy=j-1;yy>=0;yy--){	for (int xx=0;xx<i;xx++){pc+=pzl[x+xx][y+yy];}
}

判断是否相同:

那么,先就来讲讲判断,我们当然可以用四个图像去判断,但是那样忒麻烦了!所以,我们只需每次四个旋转图形的最小值,所以要用string。然后放进一个set,大家知道set是可以去重的,一去重大小就不一样了,就能很轻松的判断出来了!

代码


#include<bits/stdc++.h>
using namespace std;
int N,M,mina,minb,ans=0;
string pc,mnpc,pzl[25];
set<string> jdg;
int main(){cin.tie(0);ios::sync_with_stdio(false);cin>>N>>M;mina=N,minb=M;for (int i=0;i<N;i++) cin>>pzl[i];for (int i=1;i<=N;i++){if (N%i!=0) continue;for (int j=1;j<=M;j++){if (M%j!=0) continue;pc="";jdg.clear();for (int x=0;x<N;x+=i){for (int y=0;y<M;y+=j){for (int xx=0;xx<i;xx++) for (int yy=0;yy<j;yy++) pc+=pzl[x+xx][y+yy];//赋值mnpc=pc;//mnpc是四个旋转字符串中最小的pc="";for (int xx=i-1;xx>=0;xx--) for (int yy=j-1;yy>=0;yy--) pc+=pzl[x+xx][y+yy];//旋转180度mnpc=min(mnpc,pc);pc="";if (i==j){//长方形不进行此操作for (int yy=0;yy<j;yy++) for (int xx=i-1;xx>=0;xx--) pc+=pzl[x+xx][y+yy];//旋转90度mnpc=min(mnpc,pc);pc="";for (int yy=j-1;yy>=0;yy--) for (int xx=0;xx<i;xx++) pc+=pzl[x+xx][y+yy];//旋转270度mnpc=min(mnpc,pc);pc="";}jdg.insert(mnpc);//set放入每一片最小值}}if (jdg.size()==N*M/i/j){//如果相同就会去重,因此size会不相等ans++;if ((i*j<mina*minb)||(i<mina&&i*j==mina*minb)) mina=i,minb=j;//判断最小}}}cout<<ans<<endl;cout<<mina<<" "<<minb<<endl;return 0;
}

今天的题解就到这里了!大家喜欢的点个赞,谢谢!

更多推荐

CF54B Cutting Jigsaw Puzzle题解

本文发布于:2024-02-12 00:12:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1684512.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:题解   Cutting   CF54B   Puzzle   Jigsaw

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!