状态压缩dp入门 第一题 POJ 3254 Corn Fields
| Time Limit:?2000MS | ? | Memory Limit:?65536K |
| Total Submissions:?6460 | ? | Accepted:?3436 |
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 0Sample Output
9Hint
Number the squares as follows:1 2 34 ?
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.
題意:農夫FJ有一塊n行m列的矩形土地, 有的土地是肥沃的,可以在這些土地上放牛(用1表示),有的土地不能放牛(用0表示),而且相鄰的可以放牛的格子不能同時有牛,問FJ一共有多少種放牛的方案(一頭牛都不放也是一種方案)。
分析:以樣例為例,我們可以一行一行的考慮。假如對于每一行,用1表示放牛,0表示不放牛,
第一行的狀態為:000001010011(舍棄) 100101110(舍棄)111(舍棄)
符合題意的狀態只有5個,所以第一行有5種方案。
第二行的狀態為:000010
但是第二行中的010和第一行中的010沖突,所以如果第二行狀態為010時,共有4種方案;狀態為000時,有5種方案,所以一共有4+5=9種方案。
分析完我們會發現,對于每一行,都可以一個01串來表示這一行的狀態,而這個狀態可以用一個十進制整數來代替,也就是說,把這個狀態壓縮成了一個十進制整數,所以稱為是狀態壓縮。
本題中,dp[i][j] 就表示第i行狀態為j時的方案數。
狀態壓縮dp中最常見的就是位運算,因為位運算可以很方便的判斷當前狀態是否合法。例如本題中判斷第i行是不是有兩塊相鄰的土地同時都有牛,假設當前狀態為X,那么只需要判斷X&(X>>1)或者X&(X<<1)的結果是不是0,如果是0,說明沒有相鄰的,否則就說明有相鄰的。
#include<stdio.h> #include<string.h> #include<vector> using namespace std; #define mod 100000000 const int N = 1<<12 + 4; int dp[15][N], Map[15][15]; int n, m; vector<int> vec[14]; int Pow(int a, int x) //2的X次方 {int s = 1;for(int i = 1; i <= x; i++)s <<= 1;return s; } int fun(int x) //求第X行的土地狀態,0表示可以放牛,1表示不能放牛 {int s = 0;for(int i = 1; i <= m; i++)s += (!Map[x][i]) * Pow(2, m-i);return s; } int main() {int i, j;while(~scanf("%d%d",&n,&m)){memset(dp, 0, sizeof(dp));memset(vec, 0, sizeof(vec));for(i = 1; i <= n; i++)for(j = 1; j <= m; j++)scanf("%d",&Map[i][j]);vec[0].push_back(0);int k = 1<<m;for(i = 0; i < k; i++)dp[0][i] = 1;for(i = 1; i <= n; i++){int tmp = fun(i); //當前行的狀態for(j = 0; j < k; j++){if(j & (j>>1)) continue; //j的二進制表示中有兩個相鄰的1if(j & tmp) continue; //排除在當前行不符合條件的vec[i].push_back(j);}for(j = 0; j < vec[i].size(); j++) //排除和上一行沖突的{int u = vec[i][j];for(int z = 0; z < vec[i-1].size(); z++){int v = vec[i-1][z];if(u & v) continue;dp[i][u] = (dp[i][u] + dp[i-1][v]) % mod;}}}int ans = 0;for(i = 0; i < k; i++)ans = (ans + dp[n][i]) % mod;printf("%d\n",ans);}return 0; }
代碼二:
/* dp[i][j] 表示第i行狀態為j時的合法狀態數量 */#include <cstdio> #include <cstring> const int N = 13; #define mod 100000000 int dp[N][1<<N]; int beg[N];bool checkA(int x) { //判斷本行是否合法return !(x & (x >> 1)); }bool checkB(int a, int b) { //判斷和上一行是否沖突return !(a & b); }int main() {int n, m, t;while(~scanf("%d%d", &n, &m)) {memset(dp, 0, sizeof(dp));memset(beg, 0, sizeof(beg));for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {scanf("%d", &t);if(t) beg[i] = beg[i] | (1 << j);}}for(int i = 0; i < (1<<m); i++) //求出第一行的合法狀態數目if((beg[0]|i) == beg[0] && checkA(i))dp[0][i] = 1; for(int i = 1; i < n; i++) {for(int j = 0; j < (1<<m); j++) { //枚舉本行狀態if(((beg[i]|j) == beg[i]) && checkA(j)) {for(int k = 0; k < (1<<m); k++) { //枚舉上一行的狀態if(checkB(j, k)) //根據上一行遞推出本行dp[i][j] = (dp[i][j] + dp[i-1][k]) % mod;}}}}int ans = 0;for(int i = 0; i < (1<<m); i++)ans = (ans + dp[n-1][i]) % mod;printf("%d\n", ans);}return 0; }總結
以上是生活随笔為你收集整理的状态压缩dp入门 第一题 POJ 3254 Corn Fields的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 支撑百万级并发,Netty如何实现高性能
- 下一篇: BNUOJ 34978 汉诺塔