题目大意
输入n、w、h(1≤n≤10,1≤w,h≤n),求能放在w*h网格里的不同的n连块的个数(注意,平移、旋转、翻转后相同的算作同一种)。例如,2*4里的5连块有5种(第一行),而3*3里的8连块有以下3种(第二行)。
题目解析
本题难点在于如何判重,如果采用最简单的写法,每个n连块都会被重复枚举很多次。那么有没有好的方法只判重一次呢?答案是当然有的呀,毕竟人类的智慧是无穷无尽的。那么是什么方法的,现在由主角Redelmeier发现登场。
如何用数据结构表示图形呢?
其实很简单啦,我们关心的使图形的形状而不是位置,每一个图形都是由若干个格子联通,那么我们只需要记录下n个格子的位置,其整体表示就是图形的形状啦。对于格子位置的保存,不必造轮子啦,只需要用我们强大的set<Cell>就好啦。
那么现在来解决的三大难题之图形的平移、旋转、翻转
1、对于平移操作,我们可以定义一个normalize函数,找出x,y分别的最小值minX,minY,那么它可以视为一个平移矢量 (minX,minY),将连通块的每个单元格都减去该矢量,即实现了标准化。
2、对于旋转操作,我们可以定义一个rotate函数,表示将整个连通块围绕坐标原点顺时针旋转90度。如何实现呢?只需要将每个格子都顺时针旋转90度即可。相应的几何变换为(x,y)->(y,-x)。
3、对于翻转操作,由于既可以沿x轴翻转,也可以沿y轴翻转,但实际上沿x轴翻转后再绕坐标原点顺时针旋转180度即可得到沿y轴翻转的图案。因此这里我们定义一个flip函数,表示将一个连通块沿x轴翻转。相应的几何变换为(x,y)->(x,-y)。
那么如何推出n个格子组成的图形有多少种呢?
当n>1时,一定是在n-1连通块的基础上生成的,即以每个n-1连通块为基础,以某一个n-1 连通块的某个单元格开始,向上下左右4个方向扩展。如果可以扩展,且不出现重复,就找到了一个n连通块,加入到集合中来。最终完成n连通块的枚举。(这也是我们的打表技术啦)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
struct Cell{
int x, y;
Cell(int x = 0, int y = 0):x(x), y(y){}
bool operator < (const Cell rhs) const {
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
};
typedef set<Cell> Polyomino;
#define FOR_CELL(c, p) for(Polyomino::const_iterator c = (p).begin(); c != (p).end(); ++c)
inline Polyomino normalize(const Polyomino &p){ //标准化
Polyomino q;
int minX = p.begin()->x, minY = p.begin()->y;
FOR_CELL(c, p){
minX = min(minX, c->x);
minY = min(minY, c->y);
}
FOR_CELL(c, p){
q.insert(Cell(c->x - minX, c->y - minY));
}
return q;
}
inline Polyomino rotate(const Polyomino &p){ //顺时针旋转90度
Polyomino q;
FOR_CELL(c, p){
q.insert(Cell(c->y, -c->x));
}
return normalize(q);
}
inline Polyomino flip(const Polyomino &p){ //向x轴翻转
Polyomino q;
FOR_CELL(c, p)
q.insert(Cell(c->x, -c->y));
return normalize(q);
}
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
const int maxn = 11;
set<Polyomino> Poly[maxn];
int ans[maxn][maxn][maxn];
inline bool flipInFourDir(Polyomino &p, const int &n){ //360 = 90 * 4度旋转
for(int i = 0; i < 4; ++i){
if(Poly[n].count(p) != 0) return false;
p = rotate(p);
}
return true;
}
void checkPolyomino(const Polyomino &p, const Cell &c){
Polyomino q = p;
q.insert(c);
q = normalize(q);
int n = q.size();
if(flipInFourDir(q, n) == false) return;
q = flip(q);
if(flipInFourDir(q, n) == false) return;
Poly[n].insert(q);
}
void generate(){
Polyomino p;
p.insert(Cell(0, 0));
Poly[1].insert(p);
for(int n = 2; n < maxn; ++n){
for(set<Polyomino>::iterator s = Poly[n - 1].begin(); s != Poly[n - 1].end(); ++s){
FOR_CELL(c, *s){
for(int i = 0; i < 4; ++i){
Cell newc(c->x + dx[i], c->y + dy[i]);
if((*s).count(newc) == 0) checkPolyomino(*s, newc);
}
}
}
}
for(int n = 1; n < maxn; ++n){
for(int w = 1; w < maxn; ++w){
for(int h = 1; h < maxn; ++h){
int cnt = 0;
for(set<Polyomino>::iterator s = Poly[n].begin(); s != Poly[n].end(); ++s){
int maxX = 0, maxY = 0;
FOR_CELL(c, *s){
maxX = max(maxX, c->x);
maxY = max(maxY, c->y);
}
if(min(h, w) > min(maxX, maxY) && max(h, w) > max(maxX, maxY)) ++cnt;
}
ans[n][w][h] = cnt;
}
}
}
}
int main(){
generate();
int n, w, h;
freopen("data.in", "r", stdin);
while(scanf("%d%d%d", &n, &w, &h) == 3){
printf("%d\n", ans[n][w][h]);
}
return 0;
}