【算法竞赛入门经典】7.7 回溯法求连通块 例题7-14 UVa1602

本文介绍了如何使用回溯法解决寻找适合矩形的n-polyominoes问题,详细分析了平移标准化、旋转和翻转的判重策略,并提供了问题的存储结构和样例实现代码。
摘要由CSDN通过智能技术生成

【算法竞赛入门经典】7.7 回溯法求连通块 例题7-14 UVa1602

例题UVa11211

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.
这里写图片描述

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

分析。

很直截了当的一个回溯法求连通块的问题。从只有一个小格子开始,每次放置一个新的块上去,然后再判重。
最重要的是判重。鉴于每次旋转翻转或是加上格子之后可能出现的情况千奇百怪,所以为了使相同的组合成功判重,需要进行如下操作:
1.平移标准化
将每一种多边形的所有格子减去最小X,最小Y,这样就完成了平移的初始化。
2.旋转
实际上,一个多边形标准化之后,绕着原点进行旋转,每次旋转之后,多边形的每一个格子的坐标,y就是原来的-x,x就是原来的y,这是顺时针旋转的情况。
注意,旋转之后一定要平移标准化再用来判重。
3.翻转
实际上,沿着x轴或者y轴翻转的图形是同一个多边形。任意旋转180°就得到了另一个。所以,只需要翻转一个再标准化即可。例如,x不变,y变为-y。

本题储存结构

利用结构体Cell储存每一个组成多边形的格子。
利用set<Cell&gt ; poly来表示每

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值