UVA-1602 网络动物 Lattice Animals

参考别人的题解(建议康康思路),完全基于刘汝佳老师的代码,将我的理解写进了注释,供大家参考

(ps:因为我的阅读习惯是从main一点点按执行顺序看,所以建议按我的阅读习惯来看,这样注释可能比较明白)

// 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) {};
    // set依赖<判重,故需要重载
    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)// 调用参数需要加括号,返回参数则不用

/* 标准化函数
 * 在调用前,加入了一个新的方块,也即一个Cell(x,y)坐标
 * 标准化即将图像*p向左下平移到最底(最贴近坐标轴):x-minX,y-minY
 */
inline Polyomino normalize(const Polyomino &p) {
    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;
}

// 以原点为中心,顺时针90度旋转
inline Polyomino rotate(const Polyomino &p) {
    Polyomino p2;
    FOR_CELL(c, p)
        p2.insert(Cell(c->y, -c->x));// !负
    return normalize(p2);
}

// 以x轴为轴翻转
inline Polyomino flip(const Polyomino &p) {
    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里加入一个方块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[1].insert(s);

    // generate
    for(int n = 2; n <= maxn; n++) {
        // poly[i]==set<Polyomino> 同一连通块数量下 (x,y)的集合  的集合——即拼成的图形的集合
        // *p==Polyomino (x,y)的集合——即拼成一个图形
        // *c==Cell (x,y)——即图形的每个方块
        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]);
                    // 检查拓展的方块是否与原来的重叠,否,则进入check函数
                    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);
                    }
                    // 图形*p在h,w框框(范围)内
                    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值