Algorithm/state-compress-dp/main.cpp

64 lines
1.4 KiB
C++
Raw Normal View History

#include <iostream>
#include <vector>
using namespace std;
int m, n;
int obstacle[101];
int dp[101][1025];
const int MOD_FACTOR = 100000007;
vector<int> state;
void init_state() {
state.push_back(0);
for (int i = 0; i < (1 << n); i++) {
if (!(i & i << 1)) {
state.push_back(i);
}
}
}
void calculate() {
for (int j = 1; j < state.size(); j++) {
if (!(state[j] & obstacle[1])) { // Check overlap
dp[1][j] = 1;
}
}
for (int i = 2; i <= m; i++) {
for (int j = 1; j < state.size(); j++) {
if (!(state[j] & obstacle[i])) { // Check obstacle overlap
for (int k = 1; k < state.size(); k++) {
if (!(state[j] & state[k])) { // Check overlap
dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD_FACTOR;
}
}
}
}
}
}
int main() {
cin >> m >> n;
for (int i = 1; i <= m; i++) {
obstacle[i] = 0;
int num;
for (int j = 1; j <= n; j++) {
cin >> num;
if (num == 0) {
obstacle[i] += (1 << (n - j));
}
}
}
init_state();
calculate();
int ans = 0;
for (int j = 1; j < state.size(); j++) {
ans = (ans + dp[m][j]) % MOD_FACTOR;
}
cout << ans - 1 << endl;
return 0;
}