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连通块不同形态的个数. 思路: 实在是不知道该怎么做好,感觉判重实在是太麻烦了. 判重就是判断所有格子位置是否都相同 ...
随机推荐
- ueditor表格边框没有颜色的解决
问题: 用ueditor画表格,会发现表格存在,但是表格边框没有颜色. 解决方法: 需要对js文件中的样式进行修改,这里我引用的编辑器样式文件是ueditor.all.min.js,所以先找到该文件, ...
- 计算机改名引发的ORA
近期上班时,由于开机时老是提示" 局域网出现计算机重名冲突",于是把计算机名字给改了,从PC2010081312zeo改为了CXBIKKKKKKK,结果第二天来的时候,用PL/SQ ...
- wget实例
问题:下载http://web.utah.edu/thorne/computing/网页中所有的文件 命令:wget -c -r -np -k -L -p http://web.utah.edu/th ...
- 2018-02-02-解决IDE中无法忽略的非代码文件
layout: post title: 2018-02-02-解决IDE中无法忽略的非代码文件 key: 20180202 tags: GIT 版本管理 modify_date: 2018-02-02 ...
- input里面placeholder水平居中
<style type="text/css">input::-ms-input-placeholder{text-align: center;}input::-webk ...
- 跟我一起读postgresql源码(十六)——Executor(查询执行模块之——control节点(下))
5.ModifyTable节点 先看一个ModifyTable节点的例子: postgres=# explain update test_01 set id = 5 where name = 'xxx ...
- Designing Data-Intensive Applications
下面是这本书序言中的大部分内容,本人的英文水平有限,有理解不到位的地方还请大家指教,这算是自己对这本书的读书笔记和总结. 数据是当今系统设计中许多挑战的中心,一些难以解决的问题如系统的可扩展性,一致性 ...
- [搬运] DotNetAnywhere:可供选择的 .NET 运行时
原文 : DotNetAnywhere: An Alternative .NET Runtime 作者 : Matt Warren 译者 : 张很水 我最近在收听一个名为DotNetRock 的优质播 ...
- JetBrains Pycharm 破解+汉化
准备材料: 1.JetBrains Pycharm 传送门: https://pan.baidu.com/s/1qZCpIR6 密码: rvrn 2.JetbrainsCrack-2.6.2. ...
- RHEL7.2安装
先在系统启动的时候按下Del键(有些系统是F2键)进入BIOS,设置从光盘启动. 系统只有2个USB口时,1个要接光驱,另外1个口不能同时接键盘和鼠标,可以接1个USB集线器,键盘和鼠标同时接入到集线 ...