对紫书的代码加了注释,参考了 https://blog.csdn.net/ecnu_lzj/article/details/52830231的文章 主要是模块化各个功能、打表的思想 SET判重的思想 和标准化联通块的思想
// UVa1602 Lattice Animals
// Rujia Liu
#include<cstdio>
#include<cstring>
#include<algorithm>
#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) {//将图形标准化,即从(0,0)开始
int minX = p.begin()->x, minY = p.begin()->y;
FOR_CELL(c, p) {
minX = min(minX, c->x);
minY = min(minY, c->y);
}
Polyomino p2;
FOR_CELL(c, p)
p2.insert(Cell(c->x - minX, c->y - minY));
return p2;
}
inline Polyomino rotate(const Polyomino &p) {//顺时针旋转90° 即 x y互换位置
Polyomino p2;
FOR_CELL(c, p)
p2.insert(Cell(c->y, -c->x));
return normalize(p2); //返回标准化的
}
inline Polyomino flip(const Polyomino &p) {//关于x对称 即 X不变 Y为相反数
Polyomino p2;
FOR_CELL(c, p)
p2.insert(Cell(c->x, -c->y));
return normalize(p2);//返回标准化的
}
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,-1,1};
const int maxn = 10;
set<Polyomino> poly[maxn+1];//联通快集合
int ans[maxn+1][maxn+1][maxn+1];//打表的答案
// add a cell to p0 and check whether it's new. If so, add to the polyonimo set
void check_polyomino(const Polyomino& p0, const Cell& c) {
Polyomino p = p0;
p.insert(c);
p = normalize(p);
int n = p.size();
for(int i = 0; i < 4; i++) { //旋转后看有没有重复的
if(poly[n].count(p) != 0) return;
p = rotate(p);
}
p = flip(p);
for(int i = 0; i < 4; i++) { //折叠后再旋转看有没有重复的
if(poly[n].count(p) != 0) return;
p = rotate(p);
}
poly[n].insert(p);
}
void generate() {
Polyomino s;
s.insert(Cell(0, 0));//一个连通快就只有一个点 poly[n]表示n连通的集合
poly[1].insert(s);
// generate
for(int n = 2; n <= maxn; n++) {//n连通由n-1连通推出
for(set<Polyomino>::iterator p = poly[n-1].begin(); p != poly[n-1].end(); ++p)
FOR_CELL(c, *p)
for(int dir = 0; dir < 4; dir++) {
Cell newc(c->x + dx[dir], c->y + dy[dir]);//不用管越不越界 这是找出所有情况后打表
if(p->count(newc) == 0) check_polyomino(*p, newc);
}
}
// precompute answers
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 p = poly[n].begin(); p != poly[n].end(); ++p) {//打表看各个连通快的个数
int maxX = 0, maxY = 0;
FOR_CELL(c, *p) {
maxX = max(maxX, c->x);
maxY = max(maxY, c->y);
}
if(min(maxX, maxY) < min(h, w) && max(maxX, maxY) < max(h, w))
++cnt;
}
ans[n][w][h] = cnt;
}
}
int main() {
generate();
int n, w, h;
while(scanf("%d%d%d", &n, &w, &h) == 3) {
printf("%d\n", ans[n][w][h]);
}
return 0;
}