天天看點

POJ —— 3254 Corn Fields(狀态壓縮dp)

題目連結:http://poj.org/problem?id=3254

題目:

Corn Fields

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17942 Accepted: 9451

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.

題目描述:

    狀态壓縮動态規劃題,算是入門題吧,狀态壓縮整體思想不難,說白了就是把多個狀态進行壓縮處理,比如可以用一個十進制整數x的二進制形态來表示多個狀态,例如x=5,其二進制表示為101,如果用0,1來表示狀态的值,那麼x就可以表示三個狀态的值,1,0,1,這個思想還是比較容易想到的,難在于結合了動态規劃,在實作方式,狀态的選取, 還有狀态的轉移等等,我也是剛開始接觸這類題,嗯,又學習了一點新知識。

    本題給出一個n*m的01矩陣,1代表可以種植的土地,0代表不可種植的土地,另外,相鄰的格子不能同時種植,問有多少種種植方案。dp[i][j]表示第i行的j種狀态下最多有多少種方案,狀态轉移方程:dp[i][j] = dp[i][j] + dp[i - 1][k]。答案就是最後一行所有狀态的方案數之和。

代碼:

#include<stdio.h>
#include<string.h>
int q[600];
int v[15];
int dp[15][600];
const int mod = 100000000;
int main()
{
	int a,b,i,j,k;
	while(scanf("%d%d",&a,&b)!=EOF){
		for(i = 1;i <= a;i ++){
			int w;
			v[i] = 0;
			for(j = 1;j <= b;j ++){
				scanf("%d",&w);
				if(!w)    //這裡用0來表示肥沃的土地,1表示不可種植的土地,循環結束後樣例v[1]的值為0(0),v[2]的值為5(101)。
					v[i] += (1<<(b - j));
			}
		}
		int top = 0;
		for(i = 0;i < (1<<b);i++){        //這裡篩選出所有可行的狀态,相鄰格子不能同時為1。這裡的1表示種植的土地。
			if((i&(i<<1))==0)
				q[top++] = i;
		}
		memset(dp,0,sizeof(dp));
		for(i = 0;i < top;i ++){        //初始化dp數組
			if((q[i]&v[1]) == 0)    //如果目前的狀态和資料不沖突則記錄dp[1][i] = 1.
				dp[1][i] = 1;
		}
		for(i = 2;i <= a;i ++){
			for(j = 0;j < top;j ++){
				if((q[j]&v[i]) == 0){        //如果目前狀态符合目前行資料則進入
					for(k = 0;k < top;k ++){
						if(((q[k]&v[i - 1]) == 0) && ((q[k]&q[j]) == 0)){    //如果q[k]狀态和上一行i-1的資料不沖突且和q[j]不沖突則累加。
							dp[i][j] = (dp[i][j] + dp[i - 1][k])%mod;
						}
					}
				}
			}
		}
		int max = 0;
		for(i = 0;i < top;i ++){
			max = (max + (dp[a][i]%mod))%mod;
		}
		printf("%d\n",max);
	}
	return 0;
}