UVa 1602 网格动物(Lattice Animals)

代码参考刘汝佳的,因为有算法所以贴上来
Polyomino

#include<bits/stdc++.h>
using namespace std;

struct Cell {
    int x, y;
    Cell(int x = 0, int y = 0):x(x), y(y) {};
    friend bool operator < (const Cell& c1, const Cell& c2) {
        return c1.x < c2.x || (c1.x == c2.x && c1.y < c2.y);
    }
};

typedef set<Cell> Polyomino;

Polyomino normalize(const Polyomino &p) {
    int minx = p.begin() -> x;
    int miny = p.begin() -> y;
    for(auto& c: p) {
        minx = min(minx, c.x);
        miny = min(miny, c.y);
    }
    Polyomino p2;
    for(auto& c: p) {
        p2.insert(Cell(c.x - minx, c.y - miny));
    }
    return p2;
}

Polyomino rotate(const Polyomino &p) {
    Polyomino p2;
    for(auto& c:p) {
        p2.insert(Cell(c.y, -c.x));
    }
    return normalize(p2);
}

Polyomino flip(const Polyomino &p) {
    Polyomino p2;
    for(auto& 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];

void checkPolyomino(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)) return;
        p = rotate(p);
    }
    p = flip(p);
    for(int i = 0; i < 4; i++) {
        if(poly[n].count(p)) return;
        p = rotate(p);
    }
    poly[n].insert(p);
}

void generate() {
    Polyomino s;
    s.insert(Cell(0, 0));
    poly[1].insert(s);

    for(int n = 2; n <= maxn; n++) {
        for(auto& p: poly[n - 1]) {
            for(auto& c: p) {
                for(int dir = 0; dir < 4; dir++) {
                    Cell newc(c.x + dx[dir], c.y + dy[dir]);
                    if(p.count(newc) == 0) checkPolyomino(p, 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(auto& p: poly[n]) {
                    int maxX = 0;
                    int maxY = 0;
                    for(auto& 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() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    generate();
    int n, w, h;
    while(cin >> n >> w >> h) {
        cout << ans[n][w][h] << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值