Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int n, m;
  5. bool a[25][15];
  6. bool ok[25][1 << 10];
  7. bool used[25];
  8. bool can[25][1 << 10];
  9. int st[25];
  10. ll dp[25][1 << 10];
  11. int main() {
  12. ios_base::sync_with_stdio(false);
  13. //freopen("input.txt", "r", stdin);
  14. cin >> n >> m;
  15. for (int i = 0; i < n; i++) {
  16. for (int j = 0; j < m; j++) {
  17. cin >> a[i][j];
  18. }
  19. }
  20. for (int row = 0; row < n; row++) {
  21. for (int j = 0; j < m; j++) {
  22. if (a[row][j] == 1) st[row] |= (1 << j);
  23. }
  24. for (int mask = 0; mask < (1 << m); mask++) {
  25. int mask2 = (mask & st[row]);
  26. if (mask2 != st[row]) continue;
  27. can[row][mask] = true;
  28. ok[row][mask] = true;
  29. memset(used, 0, sizeof used);
  30. for (int bit = 0; bit < m; bit++) {
  31. int val1 = (mask >> bit) & 1;
  32. int val2 = (mask >> (bit + 1)) & 1;
  33. if (used[bit] || a[row][bit] || (val1 == 1)) continue;
  34. used[bit] = true;
  35. if ((bit == m - 1) || (val2 == 1)) {
  36. ok[row][mask] = false;
  37. }
  38. used[bit + 1] = true;
  39. }
  40. }
  41. }
  42. dp[0][st[0]] = 1;
  43. if (n == 1) {
  44. if (ok[0][st[0]]) cout << 1;
  45. else cout << 0;
  46. return 0;
  47. }
  48. for (int row = 0; row < n - 1; row++) {
  49. for (int mask1 = 0; mask1 < (1 << m); mask1++) {
  50. if (!can[row][mask1]) continue;
  51. for (int mask2 = 0; mask2 < (1 << m); mask2++) {
  52. if ((mask1 & mask2) != 0) continue;
  53. if ((mask2 & st[row + 1]) != 0) continue;
  54. int nmask = mask1 | mask2;
  55. if (!ok[row][nmask]) continue;
  56. dp[row + 1][mask2 | st[row + 1]] += dp[row][mask1];
  57. }
  58. }
  59. }
  60. ll ans = 0;
  61. for (int mask = 0; mask < (1 << m); mask++) {
  62. if (ok[n - 1][mask]) ans += dp[n - 1][mask];
  63. }
  64. cout << ans;
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement