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

POJ 3254. Corn Fields 状态压缩DP (入门级)

时间:2015-08-06 01:51:40      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9806   Accepted: 5185

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.

Source

 

 
 
        题目大意是有M×N的玉米地,但其中有些是不肥沃的,不能种植。用1来代表肥沃,0代表不肥沃。另外奶牛不喜欢挨着吃,也就是说要间隔着种植,求有几种种植方式,并将计算结果对1E8取模。
        对于0-1状态矩阵,自然而然会想到用状态压缩来做,把一行(也可以按列)的状态压缩成一个十进制数(行状态)。另种植or不种植也可以用0-1表示,并根据题目所说不能挨着种植,即这一行的某个位置种植了,下一行的同一位置就不能种植,可以知道两行的种植状态相位与要为0
        另外说一个行种植状态有效,即相邻的格子是不能种植的,需要左移一位后与自身相位与为0,如果存在相邻种植的格子,则一定会保留位1,不可能得出0的结果,据此枚举出这些状态再进行判断。
        然而并不是每个有效的行种植状态对于每一行都有效,因为有的行存在一些位置是不能种植的,用0表示。为了方便判断和计算,我们考虑将行状态取反,即0表示肥沃,1表示不肥沃,这样只有当行种植状态和行状态相位与为0,这个种植状态才在该行有效,因为如果种在了不肥沃的格子上,相位与会保留位1,结果不为0。
        于是我们有以下代码:
                ①在读入时就将格子状态取反,压缩成行状态存到row[]数组里;
                ②枚举所有有效的种植状态,存到rec[]数组里,并将最大值存进去避免后面越界;
                ③先处理第一行,给dp一个基准:对于每个有效种植状态,如果在第一行也有效,计数1次;
                ④对于剩余的行,不仅要判断每个有效种植状态,还要判断两行的种植状态有没有冲突;
                ⑤对于最后一行,把每个种植状态的计数加起来,就是总的种植方法数。
 
 1 #include <stdio.h>
 2 #define MOD 100000000
 3 int row[13], rec[377], dp[13][377];
 4 int main()
 5 {
 6     int M, N, t;
 7     scanf("%d%d", &M, &N);
 8     for(int i=0; i<M; i++)
 9         for(int j=0; j<N; j++)
10             scanf("%d", &t),                    //t=M[i][j]
11             row[i]=(row[i]<<1)|!t;              //reverse row state
12     int x=1<<12, k=0;
13     for(int i=0; i<x; i++)                      //calculate all valid states
14         if(!(i&(i<<1)))                         //is a valid row state
15             rec[k++]=i;
16     rec[k]=x;
17     x=1<<N;
18     for(int i=0; rec[i]<x; i++)                 //process first row
19         if(!(row[0]&rec[i]))
20             dp[0][i]=1;
21     for(int r=1; r<M; r++)                      //for each row
22         for(int i=0; rec[i]<x; i++)             //for each valid state
23             if(!(row[r-1]&rec[i]))              //if valid also for last row
24                 for(int j=0; rec[j]<x; j++)     //for each valid state
25                     if(!(row[r]&rec[j]))        //if valid also for this row
26                         if(!(rec[i]&rec[j]))    //and the two are not conflict
27                             dp[r][j]=(dp[r][j]+dp[r-1][i])%MOD;
28     int r=M-1;
29     for(int i=1; rec[i]<x; i++)                 //process last row
30         dp[r][0]=(dp[r][0]+dp[r][i])%MOD;
31     printf("%d\n", dp[r][0]);
32     return 0;
33 }

 

        当然有进一步的空间优化:可以考虑不开二维数组,而是用两个一维数组来交换值,或者用两个动态数组,交换指针。如果读者看懂了或自己实现了代码,就不难理解,不再详述。
 
 



POJ 3254. Corn Fields 状态压缩DP (入门级)

标签:

原文地址:http://www.cnblogs.com/BlackStorm/p/4706243.html

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