首页 > ACM题库 > HDU-杭电 > hdu 2396 Dominoes-动态规划-[解题报告]C++
2014
01-05

hdu 2396 Dominoes-动态规划-[解题报告]C++

Dominoes

问题描述 :

A domino contains two ends, each labeled with number between 1 and 6. You are to write a program that determines if a set of dominoes can be organized in a line so that all dominoes are used; numbers on successive dominoes match; and the numbers on both ends match. You are allowed to rearrange and flip the dominoes arbitrarily. For example, the five dominos: (3 3), (3 1), (4 3), (1 6), and (4 6) can be arranged as:

The five dominos: (4 5), (3 4), (1 2), (2 3), and (5 5) cannot be arranged with all ends matching.

输入:

The input contains a list of domino-sets. The first line of each set contains a single integer corresponding to the number of dominos in the set � 3 <= N <= 10. The next N lines each contain the two values on a single domino. The end of input is denoted by N = 0.

输出:

The input contains a list of domino-sets. The first line of each set contains a single integer corresponding to the number of dominos in the set � 3 <= N <= 10. The next N lines each contain the two values on a single domino. The end of input is denoted by N = 0.

样例输入:

3
1 2
3 2
3 1
5
4 5
3 4
1 2
2 3
5 5
5
3 3
3 1
4 3
1 6
4 6
0

样例输出:

Set #1: YES
Set #2: NO
Set #3: YES

Tiling Dominoes, Uva 11270, 轮廓线dp入门题

将n*m的网格分成n*m个阶段

每个阶段i*j有2^m个状态

dp[n][m][1<<m](m为min(m, n)) 这里用了滚动数组

对于每个阶段的每个状态有3种转移方式

(1)不放

(2)竖放

(3)横放

转移条件见注释

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FD(i, b, a) for(int i = (b); i >= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(a, v) memset(a, v, sizeof(a))
#define PB push_back
#define MP make_pair
typedef long long LL;
const int INF = 0x3f3f3f3f;

LL dp[2][1 << 10];
int now, next;
int n, m;
int ALL;
void update(int b, int a)///now阶段的a状态,向next阶段的b状态转移
{
    if (b & (1 << m))///保证全部被覆盖的条件
        dp[next][b ^ (1 << m)] += dp[now][a];
}

int main()
{
    while (scanf("%d%d", &n, &m) != EOF)
    {
        if (n < m) swap(n, m);
        CLR(dp, 0);
        now = 0;
        ALL = (1 << m) - 1;
        dp[0][ALL] = 1;
        REP(i, n) REP(j, m)///枚举阶段i,j
        {
            next = now ^ 1;
            for (int r = 0; r <= ALL; r++)///枚举当前阶段的状态
            {
                if (dp[now][r])
                {
                    update(r << 1, r);///不放
                    if (i && !(r & (1 << (m - 1)))) update((r << 1) ^ (1 << m) ^ 1, r);///竖着放,要求,上面的ceil为没有覆盖且i不是第一行
                    if (j && !(r & 1)) update((r << 1) ^ 3, r);///横着放,要求,左面的ceil为没有覆盖且j不是第一列
                }
            }
            CLR(dp[now], 0);
            now ^= 1;
        }
        printf("%lld\n", dp[now][ALL]);
    }
}

解题转自:http://blog.csdn.net/guognib/article/details/16343949


  1. 网站做得很好看,内容也多,全。前段时间在博客园里看到有人说:网页的好坏看字体。觉得微软雅黑的字体很好看,然后现在这个网站也用的这个字体!nice!

  2. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?

  3. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的

  4. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge

  5. 思路二可以用一个长度为k的队列来实现,入队后判断下队尾元素的next指针是否为空,若为空,则出队指针即为所求。