Corn Fields POJ - 3254 (状压DP)
2017-08-06 09:48
351 查看
Corn Fields
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
Sample Output
Hint
Number the squares as follows:
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.
分析:有两个条件——题目首先要求某些格子不满足要求。所以用二维的cun数组来存储当前的整体环境,能放则为1,不能放则为0。。然后要求相邻的牛不能放在一起。则同一行的状态里面要排除两个1在一起的情况如110001就不是合法的。
代码如下:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16079 | Accepted: 8482 |
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.
分析:有两个条件——题目首先要求某些格子不满足要求。所以用二维的cun数组来存储当前的整体环境,能放则为1,不能放则为0。。然后要求相邻的牛不能放在一起。则同一行的状态里面要排除两个1在一起的情况如110001就不是合法的。
代码如下:
/* Author:kzl */ #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; typedef long long LL; const int mod = 100000000; const int maxx = (1 << 13) + 5; LL dp[13][maxx]; int cun[13][13]; void init() { memset(dp, 0, sizeof(dp)); } bool judge(int num) {//判断是否有两个1在一起,如果有则不满足条件 if(num & (num << 1))return false; else return true; } bool pan(int x, int num) {//判断num的状态是否满足第x-1行的环境要求。 x--; int kk = 0; while(num) { //num%2==1表示当前行的这一列放了牛,cun[x][kk]==0表示当前行的环境要求这一列不能放。产生矛盾,返回false。 if(num % 2 == 1 && cun[x][kk] == 0)return false; kk++; num = (num >> 1); } return true; } int n, m; int main() { while(scanf("%d%d", &m, &n) != EOF) { init(); for(int i = 0; i < m; i++)for(int j = 0; j < n; j++)scanf("%d", &cun[i][j]); for(int i = 0; i < (1 << n); i++) {//预处理第一行 if(judge(i)&&pan(1,i))dp[1][i] = 1;//判断第一行的i状态是否满足条件。 } for(int i = 2; i <= m; i++) { for(int j = 0; j < (1 << n); j++) {//枚举当前行的状态 for(int k = 0; k < (1 << n); k++) {//枚举前一行的状态。 //j和K都要满足同一行不能有相邻的1。j&k==0表示上下两个相邻行不能有相邻的1.pan(i,j)判断j状态是否满足i-1行的环境。 if(judge(j) && judge(k) && ((j & k) == 0) && pan(i, j) && pan(i - 1, k)) dp[i][j] = (dp[i][j] + dp[i - 1][k]) % mod; //dp[i][j]表示如果第i行为J状态则总共有多少种放置方式。当然递推就是要加上前一行的合法状态的放置数。 } } } LL ans = 0; for(int i = 0; i < (1 << n); i++)ans = (ans + dp[m][i]) % mod;//统计最后一行的总放置数。 printf("%lld\n", ans); } return 0; }
相关文章推荐
- Corn Fields POJ - 3254 (状压dp)
- Corn Fields POJ - 3254 状态压缩dp入门
- DP:Corn Fields(POJ 3254)
- poj Corn Fields 3254 状态压缩dp
- 2017-07-04(状压DP:POJcorn field 3254)
- poj 3254 Corn Fields //状态压缩DP
- poj 3254 Corn Fields dp状态压缩
- Corn Fields POJ - 3254(状态压缩dp入门)
- poj&nbsp;3254&nbsp;Corn&nbsp;Fields(状态压缩DP…
- POJ 3254 Corn Fields(状压DP)
- POJ 3254 Corn Fields (状压DP)
- poj 3254 Corn Fields(他们说是状压dp??)
- POJ 3254 Corn Fields(状压dp)
- POJ-3254-Corn Fields(状压DP)
- poj 3254 Corn Fields 状压dp
- POJ - 3254 - Corn Fields (状压DP)
- Poj 3254 Corn Fields(状压DP)
- POJ 3254 状压dp
- Poj 3254 Corn Fields (状压dp初开始)
- POJ 3254 Corn Fields(状压dp)