Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14753 | Accepted: 7727 |
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
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
Sample Input
2 3 1 1 1 0 1 0
Sample Output
9
Hint
1 2 34
边看题解边敲的,算是入门的第一道模板题吧。
#include <cstdio> #include <cstring> using namespace std; #define EPS 1e-7 #define clr(x) memset(x, 0, sizeof(x)) #define long long ll #define double db #define PI acos(-1.0) const int INF = 0x3f3f3f3f; const int MOD=100000000; const int MAXN = 10000000; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; const int N = 13; const int M = 1 << N; int st[M], mp[M]; int dp[N][M]; bool judge1(int x) { return (x & (x << 1)); } bool judge2(int i, int x) { return (mp[i] & st[x]); } int main() { int n, m, x; while(~scanf("%d %d", &n, &m)) { clr(st); clr(mp); clr(dp); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { scanf("%d", &x); if(x == 0) mp[i] += (1 <<(j - 1)); } } int cnt = 0; for(int i = 0; i < (1 << m); i++) { if(!judge1(i)) { st[cnt++] = i; } } for(int i = 0; i < cnt; i++) if(!judge2(1, i)) dp[1][i] = 1; for(int i = 2; i <= n; i++) { for(int j = 0; j < cnt; j++) { if(judge2(i, j)) continue; for(int k = 0; k < cnt; k++) { if(judge2(i - 1, k)) continue; if(!(st[j] & st[k])) dp[i][j] += dp[i - 1][k]; } } } int ans = 0; for(int i = 0; i < cnt; i++) { ans += dp[n][i]; ans %= MOD; } printf("%d\n", ans); } return 0; }