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 (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
随机推荐
- Object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects&qu ...
- Eclipse中切换GIT分支
切换GIT分支: 右击项目——Team——Switch To——选择你要切换的分支.
- 02操控奴隶——掌握它的语言“Python”
一 编程常识 1编程语言的发展史 程序员是计算机的主人,主人与奴隶沟通的介质是编程语言,编程语言从诞生到现在它经历了那几个阶段呢? 2 语言的特性: 3 初期的编程语言更多的是站在计算机的角度去设计编 ...
- 树莓派(Raspberry Pi):完美的家用服务器
出处:http://linux.cn/thread/11884/1/1/ 树莓派(Raspberry Pi):完美的家用服务器 自从树莓派发布后,所有在互联网上的网站为此激动人心的设备提供了很多有趣和 ...
- [学习笔记] CS131 Computer Vision: Foundations and Applications:Lecture 4 像素和滤波器
Background reading: Forsyth and Ponce, Computer Vision Chapter 7 Image sampling and quantization Typ ...
- C/C++ 名正则言顺
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50532701 名称所表达的含义极其丰富 ...
- 分库代价高的情况下,如何优化ES解决亿级数据量检索
数据平台已迭代三个版本,从一开始遇到很多常见的难题,到现在终于有片段时间整理一些已完善的文档,在此分享以供所需朋友的实现参考,但愿能帮助大家少走些弯路,在此篇幅中偏重于ElasticSearch的优化 ...
- COGS——T 1175. [顾研NOIP] 旅游电车
http://www.cogs.pro/cogs/problem/problem.php?pid=1175 ★★☆ 输入文件:buss.in 输出文件:buss.out 简单对比时间限制: ...
- Objective-C 和 Core Foundation 对象相互转换
iOS同意Objective-C 和 Core Foundation 对象之间能够轻松的转换: CFStringRef aCFString = (CFStringRef)aNSString; NSSt ...
- 字符串的HashCode可能相同
字符串的HashCode可能相同 学习了:http://blog.csdn.net/hl_java/article/details/71511815