UVa1602 - Lattice Animals

输入n,w,h(1<=n<=10,1<=w,h<=n),求能放在w*h网格里不同的n连块的个数。

回溯求解,首先确定搜索对象,格子连通,所以把连通块作为搜索对象,每次枚举一个位置,然后放一个新的块,最后重判。

每个连块会被枚举很多次,有方法可以确保每个n连块恰好被枚举一次。

用函数generate()把每种情况枚举出来。

#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){
    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)
{
    Polyomino p2;
    FOR_CELL(c,p)
    p2.insert(Cell(c->y,-c->x));
    return normalize(p2);
}
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 ans 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[1].insert(s);
    ///generate
    for(int n=2;n<=maxn;n++){
        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 news(c->x+dx[dir],c->y+dy[dir]);
                if(p->count(news)==0) check_polyomino(*p,news);
            }
    }
    ///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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值