poj 3250 状态压缩dp入门

种植玉米的多种可能性:农夫约翰的田地规划问题
农夫约翰面临一个复杂的决策问题:如何在有限的田地上选择种植玉米的位置,以避免牛群之间的近距离接触。本文详细介绍了如何通过编程解决这一问题,包括输入和输出的具体要求,以及一种高效的算法来计算所有可行的种植方案数量。

 

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7798 Accepted: 4159

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
 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<queue>
 6 #include<stack>
 7 #include<set>
 8 #include<algorithm>
 9 #include<cstring>
10 #include<stdlib.h>
11 #include<math.h>
12 #include<map>
13 using namespace std;
14 #define pb push_back
15 #define ll long long
16 #define mod 100000000
17 int dp[20][1<<13],p[20][20];
18 int can(int x){//   相邻两位是否相同
19     if(x&(x<<1)) return 0;
20     else return 1;
21 }
22 int main(){
23     int n,m;
24     while(cin>>n>>m){
25         int mmax=(1<<m)-1;
26         for(int i=1;i<=n;i++)
27             for(int j=1;j<=m;j++)
28             cin>>p[i][j];
29         memset(dp,0,sizeof(dp));
30         dp[0][0]=1;
31         for(int i=1;i<=n;i++){
32             int tmp=0;
33             for(int j=1;j<=m;j++)
34             if(!p[i][j]) tmp+=(1<<(j-1));//tmp存的是不能放牛的最大状态的值
35             for(int j=0;j<=mmax;j++){
36                 if(j&tmp) continue;//因为j的某些位与tmp中一些位置相同,但是tmp中的是不能放牛的,所以排除
37                 if(!can(j)) continue; //有相邻的位置放牛,所以排除
38                 for(int k=0;k<=mmax;k++)
39                 if(dp[i-1][k]&&!(j&k))//排除与上一行相邻的
40                 dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
41             }
42         }
43         ll ans=0;
44         for(int i=0;i<=mmax;i++) ans=(ans+dp[n][i])%mod;
45         cout<<ans<<endl;
46     }
47 }

 

转载于:https://www.cnblogs.com/ainixu1314/p/3938215.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值