状态压缩总结

ps:一年前写在qq空间的,今天把它移到这儿吧。。。

状态压缩

    好几天没写日志了,今天总结一下状态压缩。

    前几天发的那个位运算基础好好,多多看看有意。由于我的计算机组成学的不咋地,所以我的位运算有点吃力。写一下老师课件上的几个常用操作吧!

    a |= 1 << bit // 置位

    a &= ~(1 <<bit) //清位

    (a & 1 << bit) != 0 或  (a >> bit & 1) != 0    //测位

    eg : 取最后一个非0位:a& ~a  或 a & ~(a-1)

       统计非0位: for(;a;a -= a & -a) ++ant;

       取所有子集 : x = a;while (x)     x = (x-1) & a;

    以上几个我觉得比较有意思,先明白这几个再说状态压缩!

   总结一句:状态压缩的时候最容易错的就是位运算与其他的操作之间的优先级,现在我的方法就是多加括号!

   不懒了,还是baidu一下吧

http://www.cppreference.com/operator_precedence.html

Precedence Operator Description Example Associativity
1 ()
[]
->
.
::
++
--
Grouping operator
Array access
Member access from a pointer
Member access from an object
Scoping operator
Post-increment
Post-decrement
(a + b) / 4;
array[4] = 2;
ptr->age = 34;
obj.age = 34;
Class::age = 2;
for( i = 0; i < 10; i++ ) ...
for( i = 10; i > 0; i-- ) ...
left to right
2 !
~
++
--
-
+
*
&
(type
)
sizeof
Logical negation
Bitwise complement
Pre-increment
Pre-decrement
Unary minus
Unary plus
Dereference
Address of
Cast to a given type
Return size in bytes
if( !done ) ...
flags = ~flags;
for( i = 0; i < 10; ++i ) ...
for( i = 10; i > 0; --i ) ...
int i = -1;
int i = +1;
data = *ptr;
address = &obj;
int i = (int) floatNum;
int size = sizeof(floatNum);
right to left
3 ->*
.*
Member pointer selector
Member pointer selector
ptr->*var = 24;
obj.*var = 24;
left to right
4 *
/
%
Multiplication
Division
Modulus
int i = 2 * 4;
float f = 10 / 3;
int rem = 4 % 3;
left to right
5 +
-
Addition
Subtraction
int i = 2 + 3;
int i = 5 - 1;
left to right
6 <<
>>
Bitwise shift left
Bitwise shift right
int flags = 33 << 1;
int flags = 33 >> 1;
left to right
7 <
<=
>
>=
Comparison less-than
Comparison less-than-or-equal-to
Comparison greater-than
Comparison geater-than-or-equal-to
if( i < 42 ) ...
if( i <= 42 ) ...
if( i > 42 ) ...
if( i >= 42 ) ...
left to right
8 ==
!=
Comparison equal-to
Comparison not-equal-to
if( i == 42 ) ...
if( i != 42 ) ...
left to right
9 & Bitwise AND flags = flags & 42; left to right
10 ^ Bitwise exclusive OR flags = flags ^ 42; left to right
11 | Bitwise inclusive (normal) OR flags = flags | 42; left to right
12 && Logical AND if( conditionA && conditionB ) ... left to right
13 || Logical OR if( conditionA || conditionB ) ... left to right
14 ? : Ternary conditional (if-then-else) int i = (a > b) ? a : b; right to left
15 =
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
Assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Bitwise exclusive OR and assign
Bitwise inclusive (normal) OR and assign
Bitwise shift left and assign
Bitwise shift right and assign
int a = b;
a += 3;
b -= 4;
a *= 5;
a /= 2;
a %= 3;
flags &= new_flags;
flags ^= new_flags;
flags |= new_flags;
flags <<= 2;
flags >>= 2;
right to left
16 , Sequential evaluation operator for( i = 0, j = 0; i < 10; i++, j++ ) ... left to right

posted on 2006-06-08 09:33 brent 阅读(85101) 评论(8)  编辑 收藏 引用 所属分类: C++

以上红色的是我的标注但是我感觉还是很难记住,在不确定的情况下,最好是多多的加括号!(*^__^*) 嘻嘻……

下面实战一下,贴几个题的代码:

例题一:题目大意:M*N的棋盘用1*2的矩形覆盖,问有多少种方案。

这个题我用的是状态压缩的动态规划,其实这个题用搜索,用数学,用图论的知识都能解决,我看到过巧连的空间有用二分图做的(解决的是能否覆盖)。

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int m,n;
long long dp[20][3000];

bool cover(int s)
{
    for(int i = 0;i < n;i++)
    {
        //if(s == 4) cout << (s & (1 << i)) << "\n";
        if((s & (1 << i)))
        {
            if((i == n-1) || (!(s & (1 << (i+1)))))
            {
                return 0;
            }
            i++;
        }
    }
    return 1;
}
long long solve(int k,int s)
{
    if(dp[k][s] != -1) return dp[k][s];
    if(k == 0)
    {
        if(s == (1 << n)-1) return dp[k][s] = 1;
        else return dp[k][s] = 0;
    }
    dp[k][s] = 0;
    for(int i = 0;i < (1 << n);i++)
    {
        if((i | s) == ((1 << n) - 1) && cover(i&s))
            dp[k][s] += solve(k-1,i);
    }
    return dp[k][s];
}

int main()
{
    while(scanf("%d%d",&m,&n) && !(!n && !m))
    {
        memset(dp,-1,sizeof(dp));
        //cout << (1 << 11)-1;
        printf("%lld\n",solve(m,(1 << n) - 1));
    }
    return 0;
}

例题二:

炮兵阵地:这是一个经典的状态压缩的动态规划,题目大意就不说了,文章最后贴原题。

这个题比较经典!!!

源码是:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int solve[70][2];

int getCan()
{
    int re = 0;
    for(int i = 0;i < 1024;i++)
    {
        int m = i;
        if(((m << 1) & i) || ((m << 2) & i)) continue;
        solve[re][0] = i;
        solve[re][1] = (i & 1);
        while(m = m >> 1) solve[re][1] += (m & 1);
        //cout << i << " " << solve[re][1] << "\n";

        re++;
    }
    return re;
}
int m,n;
int map[110];
int dp[2][66][66];

bool can(int k,int x,int p,int mm)
{
    //if(m - mm == 0) k = x = 0;
    //if(m - mm == 1) x = 0;
    return !((solve[k][0] & solve[x][0]) || (solve[x][0] & solve[p][0]) || (solve[k][0] & solve[p][0]));
}
void fdp(int mm,int now)
{
    if(mm == 0)
    {
        int re = 0;
        for(int i = 0;i < 60;i++)
            for(int j = 0;j < 60;j++)
            {
                //cout << dp[1-now][i][j] << " ";
                if(dp[1-now][i][j] > 0)
                    re = max(re,dp[1-now][i][j]);
            }
        cout << re << "\n";
        return;
    }
    bool b = 1;
    for(int p = 0;p < 60;p++)
    {
        b = 1;
        if((solve[p][0] | map[mm]) !=  map[mm])
        {
             b = 0;
          }
        for(int k = 0;k < 60;k++)
        {
            dp[now][k][p] = -10000;
            if(!b) continue;
            for(int x = 0;x < 60;x++)
            {
                //cout << p << " ";

                if(can(x,k,p,mm))
                {
                    dp[now][k][p] = max(dp[1-now][x][k] + solve[p][1],dp[now][k][p]);
                }
            }
        }
    }
    fdp(mm-1,1-now);
}
int main()
{
    getCan();
    while(cin >> m >> n)
    {
        memset(map,0,sizeof(map));

        char c;
        for(int i =1;i <= m;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                cin >> c;
                map[i] = map[i] << 1;
                if(c == 'P') map[i] += 1;
            }
        }
        memset(dp,0,sizeof(dp));
        fdp(m,1);
    }
    return 0;
}

炮兵阵地的原题:


炮兵阵地

Description

司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队。一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P"表示),如下图。在每一格平原地形上最多可以布置一支炮兵部队(山地上不能够部署炮兵部队);一支炮兵部队在地图上的攻击范围如图中黑色区域所示:  

如果在地图中的灰色所标识的平原上部署一支炮兵部队,则图中的黑色的网格表示它能够攻击到的区域:沿横向左右各两格,沿纵向上下各两格。图上其它白色网格均攻击不到。从图上可见炮兵的攻击范围不受地形的影响。  
现在,将军们规划如何部署炮兵部队,在防止误伤的前提下(保证任何两支炮兵部队之间不能互相攻击,即任何一支炮兵部队都不在其他支炮兵部队的攻击范围内),在整个地图区域内最多能够摆放多少我军的炮兵部队。  

Input

第一行包含两个由空格分割开的正整数,分别表示N和M;  
接下来的N行,每一行含有连续的M个字符('P'或者'H'),中间没有空格。按顺序表示地图中每一行的数据。N  <= 100;M  <= 10。

Output

仅一行,包含一个整数K,表示最多能摆放的炮兵部队的数量。

Sample Input

5 4
PHPP
PPHH
PPPP
PHPP
PHHP

Sample Output

6



你可能感兴趣的:(ant,Class,input,Access,float,output)