实现的细节很多,学到了如何翻转、旋转、平移,get很多技巧,值得一做。

AC代码:

#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=10+5;
int ans[maxn][maxn][maxn];

struct animal{
    int x,y;
    animal(int x=0,int y=0):x(x),y(y){};
    bool operator < (const animal &p) const {
        return x<p.x||(x==p.x&&y<p.y);
    }
};
typedef set<animal> node;
#define For_animal(c,p) for(node::iterator c=(p).begin();c!=(p).end();++c)

inline node normalize(const node &p){  //normalize
    int minx=p.begin()->x,miny=p.begin()->y;
    For_animal(c,p){
        minx=min(minx,c->x);
        miny=min(miny,c->y);
    }
    node p2;
    For_animal(c,p){
        p2.insert(animal(c->x-minx,c->y-miny));
    }
    return p2;
}

inline node rotate(const node &p){  //rotate
    node p2;
    For_animal(c,p){
        p2.insert(animal(c->y,-c->x));
    }
    return normalize(p2);
}

inline node flip(const node &p){ //flip
    node p2;
    For_animal(c,p){
        p2.insert(animal(c->x,-c->y));
    }
    return normalize(p2);
}

const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,-1,1};
set<node>poly[maxn];

void add(const node &p0,const animal &newc){ //add
    node p=p0;
    p.insert(newc);
    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 solve(){
    node s;
    s.insert(animal(0,0));
    poly[1].insert(s);

    for(int n=2;n<=10;++n){
        for(set<node>::iterator p=poly[n-1].begin();p!=poly[n-1].end();++p){
           For_animal(c,*p)     //从当前联通块开始延伸
                for(int dir=0;dir<4;++dir){
                    int newx=c->x+dx[dir],newy=c->y+dy[dir];
                    animal newc(newx,newy);
                    if(p->count(newc)==0) add(*p,newc);
                }
        }
    }

    //make answer into array
    const int len=10;
    for(int n=1;n<=len;++n)
        for(int w=1;w<=len;++w)
            for(int h=1;h<=len;++h){
                int cnt=0;
                for(set<node>::iterator p=poly[n].begin();p!=poly[n].end();++p){
                    int maxx=0,maxy=0;
                    For_animal(c,*p){
                        maxx=max(maxx,c->x);
                        maxy=max(maxy,c->y);
                    }
                    if(min(maxx,maxy)<min(w,h)&&max(maxx,maxy)<max(w,h))  //这个地方是<而不是<=的原因是坐标是从0开始
                        ++cnt;
                }
                ans[n][w][h]=cnt;
            }

}

int main(){
    solve();
    int n,w,h;
    while(scanf("%d%d%d",&n,&w,&h)==3){
        printf("%d\n",ans[n][w][h]);
    }
    return 0;
}

如有不当之处欢迎指出!

UVA1602的更多相关文章

  1. UVA-1602 Lattice Animals 搜索问题(打表+set)

    题目链接 https://vjudge.net/problem/UVA-1602 紫书的一道例题,跟之前的很多题目有很多不同. 本题不像是一般的dfs或bfs这样的搜索套路,而是另一种枚举思路. 题意 ...

  2. UVA1602 Lattice Animals 网格动物 (暴力,STL)

    多联骨牌的生成办法,维基上只找到固定的骨牌fix,而free的没有找到. 于是只好写个set判重的简单枚举了. 旋转的操作,可以在坐标轴上画个点,以原点为轴心,逆时针旋转90度,新的点的坐标为(-y, ...

  3. UVA1602 Lattice Animals 搜索+剪枝

    题目大意 给出一个$w\times h$的网格,定义一个连通块为一个元素个数为$n$的方格的集合$A,\forall x\in A, \exists y\in A$,使得$x,y$有一条公共边.现要求 ...

  4. 【例题 7-14 UVA-1602】Lattice Animals

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 借鉴网上的题解的. 思路是. 用"标准化"的思想. 确定基准点(0,0) 然后假设(0,0)是第一个连通块. 然 ...

  5. 网格动物UVA1602

    题目大意 输入n,w,h(1<=n<=10,1<=w,h<=n).求能放在w*h网格里的不同的n连块的个数(平移,旋转,翻转算一种) 首先,方法上有两个,一是打表,dfs构造连 ...

  6. UVa 1602 网格动物(回溯)

    https://vjudge.net/problem/UVA-1602 题意:计算n连通块不同形态的个数. 思路: 实在是不知道该怎么做好,感觉判重实在是太麻烦了. 判重就是判断所有格子位置是否都相同 ...

随机推荐

  1. Ajax 基础笔记

    Ajax内容: 同步交互与异步交互 同步交互:客户端向服务器端发送请求,服务器端向客户端进行响应,这个过程中客户端不能做其他事情 异步交互:客户端向服务器端发送请求,服务器端向客户端进行响应,这个过程 ...

  2. 关于awk的多文件处理

    关于awk的多文件处理: awk的数据输入有两个来源,标准输入和文件,后一种方式支持多个文件,如1.shell的Pathname Expansion方式:awk '{...}' *.txt # *.t ...

  3. CentOS 7 yum 安装 MySQL5.7

    1.下载 MySQL 官方的 Yum Repository ,官网地址:https://dev.mysql.com/downloads/repo/yum/ 从 MySQL 官网选取合适的 MySQL ...

  4. HH的项链

    传送门 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链变得越 ...

  5. windows Apache服务器配置

    Apache 64位可以而32位不可以 安装Apache服务 注意: 如果没有自己设置Apache服务名,后面都可不跟-n "服务名",即采用默认的服务名称. 必须用管理员提示符打 ...

  6. java类加载时执行顺序

    源代码 class HelloA { public HelloA() { System.out.print("A"); } { System.out.print("B&q ...

  7. 使用clipboard.js实现复制内容至剪贴板

    下载插件 clipboard.js是不依赖flash,实现复制内容至剪贴板的js插件.下载clipboard.js的压缩包,根据需要选择dist目录下的压缩或未压缩版. github地址:https: ...

  8. Java中获取本地某一个目录下的所有文件和文件夹

    在从事web开发工作中,经常需要对本地某一个目录下的文件进行处理,而在这之前,我们需要做的就是获取到这个目录下的文件. String filepath = "D:\file";// ...

  9. 我的前端故事----关于前端数据&逻辑的思考

    最近重构了一个项目,一个基于redux模型的react-native项目,目标是在混乱的代码中梳理出一个清晰的结构来,为了实现这个目标,首先需要对项目的结构做分层处理,将各个逻辑分离出来,这里我是基于 ...

  10. Laravel 5.4.36 session 没有保存成功问题

    session使用注意点 工作中使用的是session默认的文件缓存  在使用过发现  session()->put("key","values")  发 ...