poj3254 Corn Fields(状压dp)

本文介绍了一个经典的算法问题——玉米田种植问题。问题描述了农夫约翰如何在一个由肥沃和贫瘠地块组成的矩形牧场中选择合适的地块种植玉米,使得相邻地块不会被同时选中。文章提供了状态压缩动态规划的解决方案,并附带完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17742 Accepted: 9340

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

分析:状态压缩DP。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int dp[13][1<<12],cur[13];//cur[i]记录原矩阵的第i行 
 6 int can[1<<12];
 7 int main()
 8 {
 9     int M,N;//M行N列 
10     //原矩阵1为可种植0为不可种植
11     scanf("%d%d",&M,&N);
12     int tot=0;
13     for(int i=0;i<(1<<N);i++)//1与1不能相邻 的所有情况 
14         if((i&(i<<1))==0) can[tot++]=i;//1为可种植0为不可种植
15         
16     for(int i=1;i<=M;i++)
17         for(int j=0;j<N;j++)
18         {
19             int num;
20             scanf("%d",&num);
21             if(num==0) cur[i]=(cur[i]|(1<<j));//把第j位变为1 
22             //把1变为0,0变为1,即0为可种植,1为不可种植
23         }
24 /*这样处理后第i行可行的情况变为(cur[i]&can[k])==0,0<=k<tot;
25 例如第i行为1 1 1,0与1调换后为0 0 0,则种植方案为1 0 1,1 0 0,0 1 0,0 0 1,0 0 0; 
26 */
27     for(int i=0;i<tot;i++)
28         if((cur[1]&can[i])==0) dp[1][can[i]]=1;//预处理第一行 
29     //cur[1]&can[i]这部分要加括号,位运算符&优先级比==低 
30     for(int i=1;i<M;i++)
31     {
32         for(int j=0;j<tot;j++)
33         {
34             if((can[j]&cur[i])==0)
35             {
36                 for(int k=0;k<tot;k++)//枚举第i+1行的情况 
37                 if((can[k]&cur[i+1])==0&&(can[j]&can[k])==0)
38                     dp[i+1][can[k]]+=dp[i][can[j]];
39             }
40         }
41     }
42     int ans=0;
43     for(int i=0;i<tot;i++)
44     {
45         ans+=dp[M][can[i]];
46         ans%=100000000;
47     }
48     printf("%d\n",ans);
49     return 0;
50 }
View Code

 




转载于:https://www.cnblogs.com/ACRykl/p/8391674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值