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 (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
随机推荐
- Python学习笔记(4)列表
2019-02-26 列表(list):①创建方法:用‘[ ]’,将数据包括起来,数据之间用逗号隔开.②空列表:empty = []③增删改查: 1)增加: a.append()方法——将元素添加到列 ...
- Vue常用的GitHub项目
Vue常用的GitHub项目(Demo案例) 应用实例 https://github.com/pagekit/pa... pagekit-轻量级的CMS建站系统 https://github.com/ ...
- Git学习总结(9)——如何构建你自己的 Git 服务器
现在我们将开始学习如何构建一个Git服务器,如何在具体的事件中写一个针对特定的触发操作的自定义Git(例如通告),如何发布你的代码到一个网站. 目前为止,用户对Git的焦点主要在Git的使用上.这篇文 ...
- 洛谷 P2700 逐个击破
P2700 逐个击破 题目背景 三大战役的平津战场上,傅作义集团在以北平.天津为中心,东起唐山西至张家口的铁路线上摆起子一字长蛇阵,并企图在溃败时从海上南逃或向西逃窜.为了就地歼敌不让其逃走,毛主席制 ...
- [CSS3] The different of Background-size between 'cover' and 'contain'
'cover': The smaller axies of image (x axies) should match smaller axies (x axies) of container. So ...
- 【MFC设置静态文本框背景为透明】
视图类中加入OnCtlColor()函数: IDC_STATIC1为静态文本框ID HBRUSH CAngleView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT n ...
- CentOS 安装SVNclient
1.检查是已经安装了svn: # rpm -qa subversion subversion-1.7.14-6.el7.x86_64 卸载旧版本号的svn: # yum remove subversi ...
- oracle 11g sql developer安装后无法使用
oracle11g安装后出现 再去官网单独下来个sql developer安装 sql developer须要jre支持
- 110_leetcode_Best Time to Buy and sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 更换oracle 集群网卡(Changing a Network Interface)
更换oracle 集群网卡(Changing a Network Interface) 假设换网卡前后 网卡名.ip,网关,子网掩码都不变的话,集群层面不许要做额外的操作. 一下操作为更换网卡后 ...