HDU 1565 方格取数(1) 状态压缩DP

题目大意:

从n*n的矩阵中取出一些数使得这些数互不相邻,问最大和为多少


大致思路:

明显的状态压缩DP,每两行之间的状态转移,这里受到内存限制只开两个数组来表示当先行和下一行来进行转移,原本想用vector来记录那两个状态之间可以转换的,但是受到内存限制还是用时间换空间了


代码如下:

Result  :  Accepted     Memory  :  8520 KB     Time  :  890 ms

/*
 * Author: Gatevin
 * Created Time:  2014/8/16 20:52:13
 * File Name: 123.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int dp[1 << 20];
int nex[1 << 20];
vector <int> yes;
//vector <int> G[1 << 20];
int n;
int a[21][21];

bool check(int x)
{
    for(int i = 0; i <= (n - 2); i++)
    {
        if(((x >> i) & 1) == 1 && ((x >> (i + 1)) & 1) == 1)
        {
            return false;
        }
    }
    return true;
}

void init(int n)
{
    for(int i = 0; i <= ( 1 << n) - 1; i++)
    {
       if(check(i))
       {
          yes.push_back(i);
       }
    }
   return; 
}

int main()
{
    while(~scanf("%d", &n))
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                scanf("%d", &a[i][j]);
            }
        }
        if(n == 1)
        {
            printf("%d\n", a[1][1]);
            continue;
        }
        memset(dp, 0, sizeof(dp));
        memset(nex, 0, sizeof(nex));
        yes.clear();
        init(n);
        for(unsigned int i = 0; i <= yes.size() - 1; i++)
        {
            for(int j = 0; j <= n - 1; j++)
            {
                if((1 << j) & yes[i])
                {
                    dp[yes[i]] += a[1][j + 1];
                    nex[yes[i]] += a[2][j + 1];
                }
            }
        }
        int dpnow = 1;
        /*for(int i = 0; i <= (1 << n) - 1; i++)
        {
            G[i].clear();
        }
        for(unsigned int i = 0; i <= yes.size() - 1; i++)
        {
            for(unsigned int j = i + 1; j <= yes.size() - 1; j++)
            {
                if((yes[i] & yes[j]) == 0)
                {
                    G[yes[i]].push_back(yes[j]);
                    G[yes[j]].push_back(yes[i]);
                }
            }
        }
        */
        while(dpnow < n)
        {
            /*for(unsigned int i = 0; i <= yes.size() - 1; i++)
            {
                int tmp = nex[yes[i]];
                for(unsigned int j = 0; j <= G[yes[i]].size() - 1; j++)
                {
                    nex[yes[i]] = max(nex[yes[i]], tmp + dp[G[yes[i]][j]]);
                }
            }
            */
            for(unsigned int i = 0; i <= yes.size() - 1; i++)
            {
                int tmp = nex[yes[i]];
                for(unsigned int j = 0; j <= yes.size() - 1; j++)
                {
                    if((yes[i] & yes[j]) == 0)
                    {
                        nex[yes[i]] = max(nex[yes[i]], tmp + dp[yes[j]]);
                    }
                }
            }
            for(unsigned int i = 0; i <= yes.size() - 1; i++)
            {
                dp[yes[i]] = nex[yes[i]];
            }
            dpnow++;
            if(dpnow == n) break;
            memset(nex, 0, sizeof(nex));
            for(unsigned int i = 0;i <= yes.size() - 1; i++)
            {
                for(int j = 0; j <= n - 1; j++)
                {
                    if(yes[i] & (1 << j))
                    {
                        nex[yes[i]] += a[dpnow + 1][j + 1];
                    }
                }
            }
        }
        int answer = 0;
        for(unsigned int i = 0; i <= yes.size() - 1; i++)
        {
            answer = max(answer, dp[yes[i]]);
        }
        printf("%d\n", answer);
    }   
    return 0;
}


你可能感兴趣的:(HDU,状态压缩dp,方格取数,1565)