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;
}


### Flat-Lattice 的概念 Flat-lattice 是一种用于处理自然语言处理任务的数据结构,特别是在涉及词典信息的任务中表现出色。这种结构能够将复杂的 lattice 展平成更易于处理的形式,同时保留原有的语义关系[^2]。 在具体实现方面,flat-lattice 可以定义为一组跨度(span),其中每个跨度代表一个标记(即字符或单词),并附带头部和尾部索引,用来指示该标记在原序列中的起始和结束位置。对于单个字符而言,其头部和尾部索引相同;而对于多字符组成的词语,则会记录下整个词语范围内的首尾位置。 ### Flat-Lattice 的实现方式 为了更好地理解 flat-lattice 如何运作,下面给出一段 Python 代码作为示例: ```python class Span: def __init__(self, start, end, label): self.start = start self.end = end self.label = label def build_flat_lattice(tokens, dictionary): spans = [] # 构建基于字面意思的初始span列表 for i, token in enumerate(tokens): span = Span(i, i+1, 'char') spans.append(span) # 添加来自词典的新spans for entry in dictionary.entries(): positions = find_sublist_positions(tokens, entry.tokens()) for pos in positions: new_span = Span(pos, pos+len(entry), entry.name) spans.append(new_span) return sorted(spans, key=lambda s: (s.start, -s.end)) ``` 此函数 `build_flat_lattice` 接收两个参数:一个是待分析的文本分词结果 `tokens` ,另一个是从外部加载得到的领域特定术语库 `dictionary` 。通过遍历这些输入数据,程序创建了一系列 Span 对象,并按照它们在原文档中的顺序进行了排序。 ### 应用场景 FLAT 模型利用了上述提到的 flat-lattice 结构,在 Transformer 编码器之上引入了一种新的位置编码机制,使得模型能够在不改变内部架构的情况下融入额外的知识源——比如预先编译好的词汇表。这种方法不仅提高了实体识别任务的表现精度,同时也保证了高效的 GPU 并行运算能力[^3]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值