码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3254 Corn Fields 状压DP

时间:2019-05-16 20:29:33      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:plm   ide   http   term   turn   describe   注意   保险   compose   

Corn Fields

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21931   Accepted: 11470

Description

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.

Input

Line 1: Two space-separated integers: M and N 
Lines 2..M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)

Output

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

Sample Input

2 3
1 1 1
0 1 0

Sample Output

9

Hint

Number the squares as follows:
1 2 3
  4  

There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.
 

题目大意:有n*m大的一个地方,1表示土地肥沃可以种植物,0表示不能种植物,问:在不许有两个植物相邻的情况下,有多少种植的方法。

分析:直接dp因为状态较多,数组很难直接表示出来,我们采用二进制状态压缩的方法来解决问题。

 

更多详细题解见https://blog.csdn.net/mengxiang000000/article/details/51075506

 

逻辑运算一定要注意优先级,最好都加一个括号更保险

 

#include<iostream>
#include<stdio.h>
#include<string.h>
#define mod 100000000
int m, n;
int a[15], dp[13][1 << 13];//a[i]表示第i行土地是否允许种植的状态
//dp[i][j]表示第i行的种植状态为j时的方案数


//判断当前种植状态是否合法,合法要符合两个条件,都是用&运算判断
//第一,要符合土地的种植条件,即只有土地允许种植才能放
//第二,同一行相邻的位置不能同时种植
int judge(int x, int y)
{
    if ((a[x] & y) != y)//土地的允许的种植状态&当前种植状态一定==当前种植状态
        return 0;
    if ((y&(y << 1)) != 0)//判断不能有相邻的1
        return 0;//啰嗦一句,逻辑运算务必要记得加括号 if(y&(y<<1)!=y)就错了,&运算记得括号
    return 1;
}
int main()
{
    while (~scanf("%d%d", &n, &m))
    {

        for (int i = 1; i <= n; i++)
        {
            a[i] = 0;
            for (int j = 1; j <= m; j++)
            {
                int temp;
                scanf("%d", &temp);
                a[i] = (a[i] << 1) + temp;//将第i行的种植状态压缩为一个十进制数a[i]
            }
        }

        //状态转移
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;//初始化,都不种也算一种方案
        for (int i = 1; i <= n; i++)//枚举所有行
        {
            for (int j = 0; j < (1 << m); j++)//枚举当前行的所有种植状态
            {
                if (judge(i, j) == 0)//判断当前行种植状态是否合法
                    continue;
                for (int k = 0; k < (1 << m); k++)//判断上下行是否相邻
                {
                    if ((j&k) != 0)
                        continue;
                    dp[i][j] = dp[i][j] + dp[i - 1][k];
                    dp[i][j] = dp[i][j] % mod;
                }
            }
        }

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

 

poj 3254 Corn Fields 状压DP

标签:plm   ide   http   term   turn   describe   注意   保险   compose   

原文地址:https://www.cnblogs.com/-citywall123/p/10877703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!