UVA - 1602 Lattice Animals : 完备信息 set

本文探讨了如何计算不同形状的自由n-多米诺数量,这些多米诺可以放入指定大小的矩形格子中。文章提供了一种不使用回溯法的方法,并通过实例解释了算法的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目点此跳转

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[n][w][h]);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值