UVA-1602 Lattice Animals 搜索问题(打表+set)
题目链接 https://vjudge.net/problem/UVA-1602
紫书的一道例题,跟之前的很多题目有很多不同。
本题不像是一般的dfs或bfs这样的搜索套路,而是另一种枚举思路。
题意:
输入n、 w、 h(1≤n≤10,1≤w,h≤n),求能放在w*h网格里的不同的n连块的个数(平移、 旋转、 翻转后相同的图形算作同一种)。
思路:
思路很明确,生成图形后判重,加入重复表或弃掉。
本题的重点就在生成和判重。
我的思路:
连通块的生成:通过维护一个int open[10][10]={0}, vis[10][10]来记录可连通的许多块和已走块,在确定下一步时向open中添加新块的连通块(自加),在回溯时删除对应的连通块(自减)。
连通块的判重:通过move()函数平移连通块的每个块使之标准化,rote()函数旋转连通块顺时针90°,mirror()函数生成连通块镜像判断重复,同时插入重复表中。
参考思路(紫书);
连通块的生成:通过向n-1个块的重复表的各连通块中加入新块生成n个块的新图。
连通块的判重:同上,只是函数名有变。
思路二:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
using namespace std;
struct Cell{
int x, y;
Cell(int x=, int y=):x(x),y(y) {}
bool operator < (const Cell &a) const{
return x<a.x || (x==a.x && y<a.y);
}
};
int maxn=, dir[][]={,,,-,,,-,};
int n, h, w, ans[][][]={}, vis[][];
typedef set<Cell> Poly;
set<Poly> state[]; inline Poly move(Poly &p){
int mx=maxn, my=maxn;
Poly p2;
for (Poly::iterator c=p.begin(); c!=p.end(); ++c){
if (mx>c->x) mx=c->x;
if (my>c->y) my=c->y;
}
for (Poly::iterator c=p.begin(); c!=p.end(); ++c)
p2.insert(Cell(c->x-mx, c->y-my));
return p2;
} inline Poly rote(Poly &p){
Poly p2;
for (Poly::iterator c=p.begin(); c!=p.end(); ++c)
p2.insert(Cell(c->y, -(c->x)));
return move(p2);
} inline Poly mirror(Poly &p){
Poly p2;
for (Poly::iterator c=p.begin(); c!=p.end(); ++c)
p2.insert(Cell(c->x, -(c->y)));
return move(p2);
} void check(Poly p, Cell &c){
p.insert(c);
p=move(p);
if (state[n].count(p)) return;
for (int i=; i<; i++){
p=rote(p);
if (state[n].count(p)) return;
}
p=mirror(p);
if (state[n].count(p)) return;
for (int i=; i<; i++){
p=rote(p);
if (state[n].count(p)) return;
}
p=move(p);
state[n].insert(p);
} void pre(void){
Poly p;
p.insert(Cell(, ));
state[].insert(p); for (n=; n<=maxn; n++)
for (set<Poly>::iterator p=state[n-].begin(); p!=state[n-].end(); ++p)
for (Poly::iterator c=(*p).begin(); c!=(*p).end(); ++c)
for (int j=; j<; j++){
Cell nc((c->x)+dir[j][], (c->y)+dir[j][]);
if (!(p->count(nc))) check(*p, nc);
}
for (n=; n<=maxn; n++){
for (set<Poly>::iterator p=state[n].begin(); p!=state[n].end(); ++p){
int maxx=, maxy=;
for (Poly::iterator c=(*p).begin(); c!=(*p).end(); ++c){
if (maxx<(c->x)) maxx=(c->x);
if (maxy<(c->y)) maxy=(c->y);
}
if (maxx>maxy) ans[n][maxx+][maxy+]++;
else ans[n][maxy+][maxx+]++;
}
}
} int show(int w, int h){
int spr=(w>h)?w:h, mnr=(w!=spr)?w:h, re=;
for (int i=; i<=spr; i++)
for (int j=; j<=mnr; j++)
if (i>=j) re+=ans[n][i][j];
return re;
} int main(void){
pre(); while(scanf("%d%d%d", &n, &h, &w)== && n)
printf("%d\n", (n==)?:show(w, h)); return ;
}
因为思路一的代码bug还没解决,等AC了就交上来。: )
UVA-1602 Lattice Animals 搜索问题(打表+set)的更多相关文章
- UVA 1602 Lattice Animals
题目 输入n.w.h($1\leqslant n \leqslant 10, 1\leqslant w,h \leqslant n$),求能放在w*h网格里的不同的n连块的个数(注意,平移.旋转.翻转 ...
- UVA - 1602 Lattice Animals (暴力+同构判定)
题目链接 题意:求能放进w*h的网格中的不同的n连通块个数(通过平移/旋转/翻转后相同的算同一种),1<=n<=10,1<=w,h<=n. 刘汝佳的题真是一道比一道让人自闭.. ...
- UVa 1602 Lattice Animals (STL && 生成n连块 && 无方向形状判重)
题意 : 给定一个 w * h 的 矩阵,在矩阵中找不同n个连通块的个数(旋转,翻转,平移算作一种) 分析 : 这题的关键点有两个 ① 生成n连块并且存储起来(因为题目是多测试用例,如果每一次都重新生 ...
- UVA1602 Lattice Animals 搜索+剪枝
题目大意 给出一个$w\times h$的网格,定义一个连通块为一个元素个数为$n$的方格的集合$A,\forall x\in A, \exists y\in A$,使得$x,y$有一条公共边.现要求 ...
- 【DFS】【打表】Lattice Animals
[ZOJ2669]Lattice Animals Time Limit: 5 Seconds Memory Limit: 32768 KB Lattice animal is a set o ...
- UVa 1583 Digit Generator --- 水题+打表
UVa 1583 题目大意:如果x加上x的各个数字之和得到y,那么称x是y的生成元. 给定数字n,求它的最小生成元 解题思路:可以利用打表的方法,提前计算出以i为生成元的数,设为d,并保存在a[d]中 ...
- UVA 11768 - Lattice Point or Not(数论)
UVA 11768 - Lattice Point or Not option=com_onlinejudge&Itemid=8&page=show_problem&categ ...
- 按enter 进行搜索 enter提交表单
//按enter 进行搜索 document.onkeydown = function(e){ var ev = document.all ? window.event : e; if(ev.keyC ...
- UVA.129 Krypton Factor (搜索+暴力)
UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
随机推荐
- Unity 需不需要再建Assets文件夹
不需要,默认所有文件都是在Assets文件夹下创建的,看不到是因为设置了单栏模式,开启双栏模式就能看到了.
- 路飞学城Python-Day21(practise)
编写程序, 如下有三点要求: 自定义用户信息数据结构, 写入文件, 然后读取出内容, 利用json模块进行数据的序列化和反序列化 e.g { "egon":{"passw ...
- 使用python备份指定目录并删除备份超过一定时长的文件
#!/usr/bin/env python #-*- coding: utf-8 -*- """ @Project:Py @author: @Email: @Softwa ...
- 笔记本安装Archlinux笔记
同步更新于wendster大佬的个人博客 搬运自我的洛谷博客 可能会不定期更新! 因为前几天给我的小炸鸡加了一根内存条:而且先前装的Xubuntu是32位的,使用极其不方便:再加上wendster大佬 ...
- windows下的ubuntu
办公用Windows确实方便,但对于开发和学习还是用Linux比较好. 在Windows下安装Linux子系统 windows10中推出了Linux子系统,这个功能对开发和学习来说真的很好,非常方便. ...
- NOIP2018提高组省一冲奖班模测训练(三)
NOIP2018提高组省一冲奖班模测训练(三) 自己按照noip的方式考,只在最后一两分钟交了一次 第一题过了,对拍拍到尾. 第二题不会.考试时往组合计数的方向想,推公式,推了一个多小时,大脑爆炸,还 ...
- Manacher(最大回文字串)
很好的讲解 注意两端的字符要不同,同时数组要开大一些 [Manacher]最长回文子串 #include<bits/stdc++.h> #define REP(i, a, b) for(r ...
- java中 flush()方法的作用
flush() 是清空,而不是刷新啊.一般主要用在IO中,即清空缓冲区数据,就是说你用读写流的时候,其实数据是先被读到了内存中,然后用数据写到文件中,当你数据读完的时候不代表你的数据已经写完了,因为还 ...
- MyBatis学习总结(8)——Mybatis3.x与Spring4.x整合
一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:create -DgroupId=me.gacl -DartifactId=spring4-myba ...
- UVA 12507 Kingdoms
D - Kingdoms Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu A kingdom ha ...