UVA1602
实现的细节很多,学到了如何翻转、旋转、平移,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的更多相关文章
- UVA-1602 Lattice Animals 搜索问题(打表+set)
题目链接 https://vjudge.net/problem/UVA-1602 紫书的一道例题,跟之前的很多题目有很多不同. 本题不像是一般的dfs或bfs这样的搜索套路,而是另一种枚举思路. 题意 ...
- UVA1602 Lattice Animals 网格动物 (暴力,STL)
多联骨牌的生成办法,维基上只找到固定的骨牌fix,而free的没有找到. 于是只好写个set判重的简单枚举了. 旋转的操作,可以在坐标轴上画个点,以原点为轴心,逆时针旋转90度,新的点的坐标为(-y, ...
- UVA1602 Lattice Animals 搜索+剪枝
题目大意 给出一个$w\times h$的网格,定义一个连通块为一个元素个数为$n$的方格的集合$A,\forall x\in A, \exists y\in A$,使得$x,y$有一条公共边.现要求 ...
- 【例题 7-14 UVA-1602】Lattice Animals
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 借鉴网上的题解的. 思路是. 用"标准化"的思想. 确定基准点(0,0) 然后假设(0,0)是第一个连通块. 然 ...
- 网格动物UVA1602
题目大意 输入n,w,h(1<=n<=10,1<=w,h<=n).求能放在w*h网格里的不同的n连块的个数(平移,旋转,翻转算一种) 首先,方法上有两个,一是打表,dfs构造连 ...
- UVa 1602 网格动物(回溯)
https://vjudge.net/problem/UVA-1602 题意:计算n连通块不同形态的个数. 思路: 实在是不知道该怎么做好,感觉判重实在是太麻烦了. 判重就是判断所有格子位置是否都相同 ...
随机推荐
- junit忽略测试方法
JUnit 提供注解 org.junit.Ignore 用于暂时忽略某个测试方法或者说整个类.因为有时候由于测试环境受限,并不能保证每一个测试方法都能正确运行. 1,方法级别上使用@ignore来注释 ...
- MySQL复制相关变量
server_id是必须设置在master和每个slave上的唯一标识ID,其取值范围 是1~4294967295之间,且同一个复制组之内不能重复 server_uuid:server_uuid会在G ...
- 【转】12 TOP Command Examples in Linux
12个top命令 1. # top 2. # top,后输入shift+O,在“Current Sort Field:”中选左边的field对应的字母进行排序. 3. # top -u tecmint ...
- 小白的.Net Core 2.0 ConsoleApp入门(keng)指南(一)
一.准备工作 准备工作很简单,甚至可以不用Visual Studio,一只.NET CORE和Runtime即可(你有考虑过世界第一IDE的感受吗) 下载:https://www.microsoft. ...
- 忽略node.js服务中favicon.icon的请求
场景 一个最简单的node.js的http服务 const http = require('http'); const server = http.createServer(function(req, ...
- css选择器:nth-child()与:nth-of-type()的差异
:nth-child()和:nth-of-type()都是Css3中的伪类选择器,其作用相似却又不完全相同. 名词解释 :nth-child()选择器匹配其父元素的第n个子元素,不论元素类型. :nt ...
- Sonar 数据库表关系整理一(rule相关)
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/7510072.html 简介:Sonar ...
- iOS-NSPredicate正则验证【三种验证方法】
1.NSPredicate验证(谓词匹配) ///验证(string:验证的字符串) + (BOOL)stringValidate:(NSString *)string{ NSString *regu ...
- Effective Java 第三版——32.合理地结合泛型和可变参数
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- 汽车之家店铺数据抓取 DotnetSpider实战[一]
一.背景 春节也不能闲着,一直想学一下爬虫怎么玩,网上搜了一大堆,大多都是Python的,大家也比较活跃,文章也比较多,找了一圈,发现园子里面有个大神开发了一个DotNetSpider的开源库,很值得 ...