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

POJ 3254 - Corn Fields - [状压DP水题]

时间:2017-10-03 12:43:18      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:rate   script   ref   ble   poj   www   lines   ted   div   

 题目链接:http://poj.org/problem?id=3254

 Time Limit: 2000MS Memory Limit: 65536K

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.

题解:

怎么说呢,类似的状压DP题解写多了真的挺无聊的,如果你已经撸完了:

http://www.cnblogs.com/dilthey/p/7623028.html

http://www.cnblogs.com/dilthey/p/7604432.html

这两道题目的话,再来看本题,真的是很水的,所以就懒得写题解了,反正就是差不多的思路;

AC代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 int m,n,mp[15];
 4 int dp[15][1<<12];
 5 int main()
 6 {
 7     while(scanf("%d%d",&m,&n)!=EOF)//m行n列
 8     {
 9         for(int i=1,tmp;i<=m;i++)
10         {
11             mp[i]=0;
12             for(int j=1;j<=n;j++)
13             {
14                 scanf("%d",&tmp);
15                 tmp=!tmp;
16                 mp[i]|=tmp;
17                 if(j!=n) mp[i]<<=1;
18             }
19         }
20 
21         memset(dp,0,sizeof(dp));
22         for(int i=0;i<(1<<n);i++)//初始化第一行
23         {
24             if( i&(i<<1) || i&mp[1] ) continue;
25             dp[1][i]=1;
26         }
27         for(int r=2;r<=m;r++)//遍历第2~m行
28         {
29             for(int i=0;i<(1<<n);i++)//枚举第r行状态
30             {
31                 if( i&(i<<1) || i&mp[r] ) continue;
32                 for(int j=0;j<(1<<n);j++)//枚举第r-1行状态
33                 {
34                     if( j&(j<<1) || j&mp[r-1] || i&j ) continue;
35                     dp[r][i]+=dp[r-1][j];
36                 }
37             }
38         }
39 
40         int ans=0;
41         for(int i=0;i<(1<<n);i++)
42         {
43             if( i&(i<<1) || i&mp[m] ) continue;
44             ans+=dp[m][i];
45         }
46         printf("%d\n",ans%100000000);
47     }
48 }

PS.哦对,有一点倒是值得提醒,需要对100000000取模,因为忘记了这个WA了一发,羞愧脸。

 

POJ 3254 - Corn Fields - [状压DP水题]

标签:rate   script   ref   ble   poj   www   lines   ted   div   

原文地址:http://www.cnblogs.com/dilthey/p/7623544.html

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