ACM题目————网格动物
Lattice animal is a set of connected sites on a lattice. Lattice animals on a square lattice are especially popular subject of study and are also known as polyominoes. Polyomino is usually represented as a set of sidewise connected squares. Polyomino with n squares is called n-polyomino. In this problem you are to find a number of distinct free n-polyominoes that fit into rectangle w×h. Free polyominoes can be rotated and flipped over, so that their rotations and mirror images are considered to be the same. For example, there are 5 different pentominoes (5-polyominoes) that fit into 2×4 rectangle and 3 different octominoes (8-polyominoes) that fit into 3×3 rectangle.

Input
The input file contains several test cases, one per line. This line consists of 3 integer numbers n, w, and h ( 1
n
10, 1
w, h
n).
Output
For each one of the test cases, write to the output file a single line with a integer number -- the number of distinct free n-polyominoes that fit into rectangle w×h.
Sample Input
5 1 4
5 2 4
5 3 4
5 5 5
8 3 3
Sample Output
0
5
11
12
3
输入n,w,h (1<=n<=10,1<=w,h<=n),求能放在w*h网格里的不同的n连块的个数(注意,平移,旋转,翻转后相同的算作同一种)。例如,2*4里的5连块有5种(第一行),而3*3里的8连块有以下3种(第二行)。
难点:
1.以每个格子来扩展。先枚举1连块,在对1连块的每个格子的4个方向进行扩展,枚举2连块,依次类推。
2.将n连块表示成n个格子的集合,将所有的n连块又表示成集合,判重任务交给set.
3.判重时要将n连块进行8个方向的旋转,并且每个n连块需要规范化(左下角的格子在(0,0)).
4.得到n连块后判断是否能放进w*h的网格中,由于n连块已经规范化,得到n连块的格子最大x,y坐标,即能盛下该n连块的长和宽。
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
#define maxn 10
// 代表一个网格节点
typedef struct cell
{
int x, y; //网格节点的坐标 // 构造函数
cell(int x, int y)
{
this->x = x;
this->y = y;
} bool operator < (const struct cell& a) const
{
return x < a.x || (x == a.x && y < a.y);
}
}cell; // 一个Polyomino就是一堆cell的集合
typedef set<cell> poly; // poly_set[i]代表有i个cell的poly集合
set<poly> poly_set[maxn+1]; // answer[n][w][h]的答案
int answer[maxn+1][maxn+1][maxn+1]; void gen_poly();
void check_poly(const poly& this_p, cell& this_c);
poly normalize(poly& p);
poly rotate(poly& p);
poly flip(poly& p); int main()
{
// 生成所有poly
gen_poly(); // printf("here\n");
int n, w, h;
while(scanf("%d %d %d", &n, &w, &h) == 3)
{
printf("%d\n", answer[n][w][h]);
}
return 0;
} int dic_x[4] = {-1,0,1,0};
int dic_y[4] = {0,1,0,-1}; // 生成所有poly
void gen_poly()
{
for(int i = 1; i <= maxn; i++)
poly_set[i] = set<poly>(); // 先生成有1个cell的poly
poly p1;
p1.insert(cell(0,0));
poly_set[1].insert(p1); // 分别根据有i-1个cell的poly集合来生成有i个cell的poly集合
for(int i = 2; i <= maxn; i++)
{
// 对每个poly中的每个cell尝试在不同的四个方向增加一个cell
for(set<poly>::iterator p = poly_set[i-1].begin(); p != poly_set[i-1].end(); p++)
{
for(poly::const_iterator q = p->begin(); q != p->end(); q++)
{
for(int j = 0; j < 4; j++)
{
cell new_c(q->x+dic_x[j], q->y+dic_y[j]);
// cell new_c;
if(p->find(new_c) == p->end())
{
// 检查形成的这个poly是否存在,如果不存在就加入
check_poly(*p, new_c);
} }
}
}
} // 对所有n,w,h生成答案
for(int i = 1; i <= maxn; i++)
{
for(int w = 1; w <= i; w++)
{
for(int h = 1; h <= i; h++)
{
int count = 0;
for(set<poly>::iterator p = poly_set[i].begin(); p != poly_set[i].end(); p++)
{
int max_x = p->begin()->x, max_y = p->begin()->y;
for(poly::iterator q = p->begin(); q != p->end(); q++)
{
if(max_x < q->x)
max_x = q->x;
if(max_y < q->y)
max_y = q->y;
} if(min(max_x, max_y) < min(w, h) && max(max_x, max_y) < max(w, h))
{
count++;
}
}
/* if(count != 0)
printf("answer[%d][%d][%d] = %d\n", i, w, h, count);
*/ answer[i][w][h] = count;
}
}
}
} // 检查形成的这个poly加上这个cell是否存在,如果不存在就加入
void check_poly(const poly& this_p, cell& this_c)
{
poly p = this_p;
p.insert(this_c);
// 规范化到最小点为(0,0)
p = normalize(p); int n = p.size();
// 检查旋转的8个方向是否存在,如果不存在就加入到poly集合
for(int i = 0; i < 4; i++)
{
if(poly_set[n].find(p) != poly_set[n].end())
return;
// 对该poly向右旋转90度
p = rotate(p);
}
// 将该poly向下反转180度
p = flip(p);
for(int i = 0; i < 4; i++)
{
if(poly_set[n].find(p) != poly_set[n].end())
return;
// 对该poly向右旋转90度
p = rotate(p);
}
poly_set[n].insert(p); } // 规范化到最小点为(0,0)
poly normalize(poly& p)
{
poly this_p;
int min_x = p.begin()->x, min_y = p.begin()->y;
for(poly::iterator q = p.begin(); q != p.end(); q++)
{
if(q->x < min_x)
min_x = q->x;
if(q->y < min_y)
min_y = q->y;
}
for(poly::iterator q = p.begin(); q != p.end(); q++)
{
this_p.insert(cell(q->x-min_x,q->y-min_y));
}
return this_p;
} // 对该poly向右旋转90度
poly rotate(poly& p)
{
poly this_p;
for(poly::iterator q = p.begin(); q != p.end(); q++)
{
this_p.insert(cell(q->y,-q->x));
}
return normalize(this_p);
} // 将该poly向下反转180度
poly flip(poly& p)
{
poly this_p;
for(poly::iterator q = p.begin(); q != p.end(); q++)
{
this_p.insert(cell(q->x,-q->y));
}
return normalize(this_p);
}
ACM题目————网格动物的更多相关文章
- ACM题目————中缀表达式转后缀
题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...
- ACM题目————食物链
题目描述 Description 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B,B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并 ...
- HDU ACM 题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
- ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]
原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...
- 有一种acm题目叫做,奇葩!
本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pi ...
- ACM题目————STL练习之求次数
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...
- UVa 1602 网格动物(回溯)
https://vjudge.net/problem/UVA-1602 题意:计算n连通块不同形态的个数. 思路: 实在是不知道该怎么做好,感觉判重实在是太麻烦了. 判重就是判断所有格子位置是否都相同 ...
- 网格动物UVA1602
题目大意 输入n,w,h(1<=n<=10,1<=w,h<=n).求能放在w*h网格里的不同的n连块的个数(平移,旋转,翻转算一种) 首先,方法上有两个,一是打表,dfs构造连 ...
- ACM题目————zoj问题
题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20322 解决:3560 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. ...
随机推荐
- Permissions 0664 for '/home/root/.ssh/id_rsa' are too open.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY @ @@@ ...
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之三---角色管理源码分享
使用Jquery+EasyUI 进行框架项目开发案例讲解之三 角色管理源码分享 在上两篇文章 <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享> ...
- C++ 基础复习 1
1. 友元 友元的作用是,友元函数内部可以直接访问外围类的private的字段或方法.通俗的理解就是解决了访问权限的问题. 1) 有点像java的内部类,但是只能在外围类中声明,定义(实现)部分要写在 ...
- Oracle自增长ID
在Oracle中,可以为每张表的主键创建一个单独的序列,然后从这个序列中获取自动增加的标识符,把它赋值给主键.例如一下语句创建了一个名为customer_id_seq的序列,这个序列的起始值为1,增量 ...
- 学习OpenCV——hand tracking手势跟踪
这几日,岛上风云突变,我这个倒霉孩子终究木有躲过感冒的魔掌,中枪鸟~~~ 这几天只写了个简单的手势跟踪的代码. 原理是:背景差分+肤色检测. 背景差分:取前30帧图像取平均值,计算前30帧之差的和,再 ...
- Tomcat系列之Java技术详解
一.概述 1.前言 在前面几篇博客中,我们和大家说了负载均衡器服务器.Web服务器.反向代理服务器.缓存服务器,从这篇博客开始我们和大家说说应用程序服务器,对于上述内容不了解的博友可以去参考一下我们前 ...
- 阶乘之和 & 程序运行时间 & 算法分析
实例:输入n,计算S = 1! + 2! + 3! + 4! + ... + n!的末六位(不含前导0).其中 n ≤ 106. 分析:考虑到数据溢出后程序如下: #include <stdio ...
- CGRect 结构体的另外一种写法
// _textF = CGRectMake(textX, textY, textSize.width, textSize.height); _textF = (CGRect){{textX, ...
- ACM之Java速成(4)
ACM中Java.进制转换 Java进制转换: 由于Unicode兼容ASCII(0-255),因此,上面得到的Unicode就是ASCII. java中进行二进制,八进制,十六进制,十进制间进行相互 ...
- 输入和输出的总结(c语言)
c语言中有多种的输入和输出方式,下面就简单总结一下: 一.输入的三种方式 (1)scanf scanf 函数可以在变量中使用,也可以在数组中使用,当然指针上也能用到,是一个很好的输入函数.scanf是 ...