首页 > 代码库 > POJ 3254 Corn Fields

POJ 3254 Corn Fields

Corn Fields

Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 3254
64-bit integer IO format: %lld      Java class name: Main
 

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 31 1 10 1 0

Sample Output

9

Source

USACO 2006 November Gold
 
解题:状压dp...
 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int mod = 100000000;18 int n,m,mp[18],s[1000],dp[18][1000],tot;19 int main() {20     int tmp;21     while(~scanf("%d %d",&n,&m)) {22         memset(mp,0,sizeof(mp));23         for(int i = 0; i < n; i++)24             for(int j = 0; j < m; j++) {25                 scanf("%d",&tmp);26                 mp[i] |= (!tmp)<<j;27             }28         for(int i = tot = 0; i < (1<<m); i++) {29             if(i&(i<<1)) continue;30             s[tot++] = i;31         }32         memset(dp,0,sizeof(dp));33         for(int i = 0; i < tot; i++)34             dp[0][i] = !(mp[0]&s[i]);35         int ans = 0;36         for(int i = 1; i < n; i++) {37             for(int j = 0; j < tot; j++) {38                 if(s[j]&mp[i]) continue;39                 for(int k = 0; k < tot; k++) {40                     if(s[j]&s[k]) continue;41                     dp[i][j] += dp[i-1][k];42                     dp[i][j] %= mod;43                 }44             }45         }46         for(int i = 0; i < tot; i++){47             ans += dp[n-1][i];48             ans %= mod;49         }50         printf("%d\n",ans);51     }52     return 0;53 }
View Code

 

POJ 3254 Corn Fields