Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6159 | Accepted: 3268 |
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
#define Max_N 13 #define STATENUM 4100 #define MOD 100000000 using namespace std; int M;//列数 int N;//行数 int cannot[Max_N];//infertile用1表示,fertile用0表示 vector<int> state; int all_state;//行中不相邻的状态数目 int dp[Max_N][STATENUM];//dp[i][j]第i行截止到状态j的情况数 bool ok(int i) { if(i & (i << 1)) return false; if(i & (i >> 1)) return false; return true; } void get_all_states() { for(int i = 0;i < (1 << M);i++) { if(ok(i)) state.push_back(i); } } int DP() { state.clear(); get_all_states(); memset(dp, 0, sizeof(dp)); for(int i = 0;i < state.size();i++)//首先确定第1行 { if(state[i]&cannot[1]) continue; dp[1][state[i]] = 1; } for(int row = 2;row <= N;row++)//从第2行开始 { for(int i = 0;i < state.size();i++)//考虑每一种状态 { if(state[i] & cannot[row]) continue; for(int j = 0;j < state.size();j++)//枚举上一行的情况 { if(state[j] & cannot[row - 1]) continue; if(state[i] & state[j])//上一行与本行不能同边 continue; dp[row][state[i]] += dp[row - 1][state[j]]; dp[row][state[i]] %= MOD; } } } int ans = 0; for(int j = 0;j < state.size();j++) ans = (ans + dp[N][state[j]]) % MOD; return ans; } int main() { //freopen("in.txt","r",stdin); while(cin >> N >> M) { for(int i = 1;i <= N;i++) { for(int j = 1;j <= M;j++) { int a; cin >> a; cannot[i] = (a == 0 ? cannot[i]|(1 << M - j) : cannot[i]); } } cout << DP() << endl; } return 0; }