状态压缩dp入门题目总结——炮兵阵地和TSP问题

Corn Fields

POJ3254 题目链接
Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can’t be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

【题目大意】:
M*N的土地,种植农作物,其中某些土地不能种植,相邻土地之间不能种植,问有多少种种植方案。

【题目思路】:
因为M ϵ [1, 12],可将这一行的种植状态用二进制数字表示,1表示种植,0表示不种植,二进制数范围为[0, 1<<12)。

这一行相邻土地不能种植可以通过M&(M<<1)是否为1来判断。
上一层土地状态S1和这一层土地状态S2 通过S1&S2是否为0表示合法。
map[i]表示i层土地可以种植的情况,通过(~map[i])&S是否为0表示这一层是否合法。

定义dp[i][S] 表示目前i行状态为S时的种植方案数。
dp[i][S]+=dp[i1][S]

【程序代码】

#include<iostream>
using namespace std;
int m, n;
int map[13];
const int mod = 100000000;
int dp[13][100000];     //dp[i][j] 放第i行 这一行状态为j  时方法数 

bool check(int x, int y)
{
    if(x&(x<<1)){   //这一行相邻放牛了 
        return false;
    }

    if((~y)&x){        //这一行不能放牛的位置上放牛了 
        return false;
    }
    return true;
}

int main()
{
    int i, j, k;
    cin >> m >> n;
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            int x;
            cin >> x;
            map[i] = map[i]*2+x;
        }
    }

    for(i=0; i<(1<<n); i++){
        if(check(i, map[1]))
        {
            dp[1][i] = 1;
        }
    }

    for(i=2; i<=m; i++)
    {
        for(j=0; j<=(1<<n)-1; j++)
        {
            if(!check(j, map[i]))
            {
                continue;
            }

            for(k=0; k<=(1<<n)-1; k++)
            {
                if(!(j&k))
                {
                    dp[i][j] += dp[i-1][k];
                    dp[i][j] %= mod;
                }
            }
        }
    }

    int ans = 0;
    for(i=0; i<(1<<n); i++)
    {
        ans += dp[m][i];
        ans %= mod;
    }

    cout << ans << endl;
    return 0;
 } 

炮兵阵地

POJ1185 题目链接

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

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

【题目思路】
比上一题限制多一些,这一次不仅是相邻了,相互之间两格之间也算,这一层的状态不仅依赖于

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值