poj 3254 状态压缩dp
2014-05-28 15:47
197 查看
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.
Source
USACO 2006 November Gold
题意:1为草地 0为空地,草地可以放牛,但相邻的草地不可以都放牛,问有多少种放牛的方法。
思路:经典状态压缩问题,预处理单行的所有不相邻的可行情况,存到ans数组中,然后在dp的时候判断上下行是否有相邻格子,用ans的下标去代替状态的话,状态数最多也就几百个,数组就不用开到2^12,大大节约了空间,还可以再用滚动数组继续优化。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7878 | Accepted: 4206 |
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
USACO 2006 November Gold
题意:1为草地 0为空地,草地可以放牛,但相邻的草地不可以都放牛,问有多少种放牛的方法。
思路:经典状态压缩问题,预处理单行的所有不相邻的可行情况,存到ans数组中,然后在dp的时候判断上下行是否有相邻格子,用ans的下标去代替状态的话,状态数最多也就几百个,数组就不用开到2^12,大大节约了空间,还可以再用滚动数组继续优化。
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int vis[13][13]; int dp[2][500]; int ans[500]; int t=0; int n,m; void init(int n) { for(int i=0; i<(1<<n); i++){ int c=i; while(!((c&1)&&(c&2))){ if(c<3){ans[t++]=i;break;} c>>=1; } } } int pd(int a,int i) { for(int j=1; j<=m; j++) if(vis[i][j]==0&&(a&(1<<(j-1))))return 0; return 1; } int pds(int a,int b) { while(a>0){ if((a&1)&(b&1))return 0; a>>=1; b>>=1; } return 1; } int main() { while(scanf("%d%d",&n,&m)!=EOF){ init(m); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&vis[i][j]); for(int i=0;i<t;i++) if(pd(ans[i],1)) dp[1][i]=1; for(int i=2;i<=n;i++){ memset(dp[i&1],0,sizeof(dp[i&1])); for(int j=0;j<t;j++) for(int k=0;k<t;k++) if(pd(ans[j],i)&&pds(ans[j],ans[k])) dp[i&1][j]+=dp[i&1^1][k]; } int sum=0; for(int i=0;i<t;i++){ sum+=dp[n&1][i]; sum%=100000000; } printf("%d\n",sum); } return 0; }
相关文章推荐
- poj 3254 状态压缩dp
- POJ3254——Corn Fields(状态压缩DP)
- POJ 3254 Corn Fields【状态压缩DP】
- [状态压缩DP] Poj 3254, Poj 1185
- POJ 3254 Corn Fields(DP + 状态压缩)
- POJ 3254 Corn Fields(状态压缩DP)
- POJ 3254 Corn Fields状态压缩DP
- poj3254 Corn Fields (状态压缩dp)
- poj 3254(状态压缩dp专题)
- poj 3254 Corn Fields(状态压缩dp)
- POJ 3254 Corn Fields(状态压缩DP)
- poj 3254 状态压缩DP
- poj 3254 Corn Fields (状态压缩dp)
- poj 3254 Corn Fields(状态压缩DP)
- POJ 3254 状态压缩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)