您的位置:首页 > 其它

UVA - 1602 Lattice Animals : 完备信息 set

2017-07-27 10:48 295 查看

题目点此跳转

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种(第二行),如图所示。

 这题参考的别人的代码,没用回溯法.

 这里直接将10*10里所有i个连通块的个数全部统计出来,然后再根据某个连通块的长宽和w,h比较,判断其是否在答案中,打表即可.

代码

#include <algorithm>
#include <iostream>
#include <sstream>
#include <utility>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#define met(a,b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
#define OT freopen("out.txt", "w", stdout);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef set<PII> Set;
const int maxn = 1e1 + 10;
const int INF = 0x7fffffff;
const int dir[5][2] = {0,0,-1,0,1,0,0,-1,0,1};
const int N = 10;

int n, w, h, ans[11][11][11];
set<Set> S[maxn];   //save all condition

void Repair(Set& x) {
int mx = INF, my = INF; Set t;
for(Set::iterator it = x.begin(); it != x.end(); ++it) {
mx = min(mx, it->first), my = min(my, it->second);
}
for(Set::iterator it = x.begin(); it != x.end(); ++it) {
t.insert(make_pair(it->first-mx, it->second-my));
}
x = t;
}

void Rotate(Set& x) {
Set t;
for(Set::iterator it = x.begin(); it != x.end(); ++it) {
t.insert(make_pair(it->second, -it->first));
}
x = t;
}
void Flip(Set& x) {
Set t;
for(Set::iterator it = x.begin(); it != x.end(); ++it) {
t.insert(make_pair(it->first, -it->second));
}
x = t;
}

void Insert(Set x, PII p) {
x.insert(p); Repair(x);
int id = x.size();
if(S[id].count(x)) return;
for(int i = 0; i < 3; ++i) {
Rotate(x); Repair(x); if(S[id].count(x)) return;
}
Flip(x); Repair(x); if(S[id].count(x)) return;
for(int i = 0; i < 3; ++i) {
Rotate(x); Repair(x); if(S[id].count(x)) return;
}
S[id].insert(x);
}

void build() {
Set t; t.insert(make_pair(0,0)); S[1].insert(t);
for(int i = 2; i <= N; ++i) {
for(set<Set>::iterator it = S[i-1].begin(); it != S[i-1].end(); ++it) {
for(Set::iterator p = (*it).begin(); p != (*it).end(); ++p) {
for(int j = 1; j <= 4; ++j) {
Set tmp = (*it);
PII pr = make_pair(p->first+ dir[j][0], p->second+dir[j][1]);
if(!it->count(pr)) Insert(*it, pr);
}
}
}
}
met(ans, 0);
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j)
for(int k = 1; k <= N; ++k) {
for(set<Set>::iterator it = S[i].begin(); it != S[i].end(); ++it) {
int mx = 0, my = 0;
for(Set::iterator p = (*it).begin(); p != (*it).end(); ++p) {
mx = max(mx, p->first); my = max(my, p->second);
}
if(min(mx, my) < min(j, k) && max(mx, my) < max(j, k)) ++ans[i][j][k];
}
}
}

int main() {
#ifdef _LOCAL
IN; //OT;
#endif // _LOCAL

build();
while(scanf("%d%d%d", &n, &w, &h) == 3) {
printf("%d\n", ans
[w][h]);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: