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 (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
随机推荐
- iOS-Core-Animation-Advanced-Techniques/12-性能调优/性能调优.md
性能调优 代码应该运行的尽量快,而不是更快 - 理查德 在第一和第二部分,我们了解了Core Animation提供的关于绘制和动画的一些特性.Core Animation功能和性能都非常强大,但如果 ...
- Dynamic dispatch mechanisms
Normally, in a typed language, the dispatch mechanism will be performed based on the type of the arg ...
- 路飞学城Python-Day46
16-如何正确的使用类选择器及总结 一般情况下尽量不要去使用id选择器,因为id选择器有很大的限制性,id一般都是JS配合使用的,类选择器都是和CSS配合使用的,特殊性情况可以用id选择器. 类的使用 ...
- BZOJ 3126 [USACO2013 Open]Photo (单调队列优化DP)
洛谷传送门 题目大意:给你一个长度为$n$的序列和$m$个区间,每个区间内有且仅有一个1,其它数必须是0,求整个序列中数字1最多的数量 神题,竟然是$DP$ 定义$f_{i}$表示第i位放一个1时,最 ...
- 硬核官宣:台积电官宣6nm及7nm加强版工艺!
台积电正式宣布了6nm(N6)工艺,在已有7nm(N7)工艺的基础上大幅度增强,号称可提供极具竞争力的高性价比,而且能加速产品研发.量产.上市速度. 这几年,曾经执行业牛耳的Intel在新工艺方面进展 ...
- python在不同情况下写入csv文件
情况一(解法一):将列表存储为csv文件.列表的每一项代表csv文件的一行. 列表中的每一项包含多个属性.list=[[属性1,属性2,属性3,……],[属性1,属性2,属性3,……],[属性1,属性 ...
- [剑指offer] 1. 二维数组中的查找 (数组)
注意是有序数组!! 思路: 1.利用二维数组由上到下,由左到右递增的规律,选取右上角或者左下角的元素a[m][n]与target进行比较, 当target小于元素a[m][n]时,那么target必定 ...
- Linux多线程实践(四 )线程的特定数据
在单线程程序中.我们常常要用到"全局变量"以实现多个函数间共享数据, 然而在多线程环境下.因为数据空间是共享的.因此全局变量也为全部线程所共同拥有.但有时应用程序设计中有必要提供线 ...
- CCControlExtension/CCControlPotentiometer
#ifndef __CCCONTROLPOTENTIOMETER_H__ #define __CCCONTROLPOTENTIOMETER_H__ #include "CCControl.h ...
- Android技术归档
各位小伙伴们.以后小巫的一些开源码都会上传到github中,所以欢迎大家Follow https://github.com/devilWwj 基于眼下我基本的技术领域在Android上,以后关于And ...