matheus_monteiro

Jogo do Preto e Branco

Sep 18th, 2021 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, m, p;
  5. int black[8][8];
  6. int dx[] = {0, 0, -1, 1};
  7. int dy[] = {1, -1, 0, 0};
  8.  
  9. // 1 -> white
  10.  
  11. bool check(int i, int j, int mask1, int mask2) {
  12.     if(black[i][j]) {
  13.         if(mask2 & (1 << j)) return false;
  14.         return true;
  15.     } else {
  16.         if(mask2 & (1 << j)) { // coloquei uma peça branca
  17.             if(i > 0 and (mask1 & (1 << j))) // cima tbm é branco
  18.                 return false;
  19.             if(j > 0 and (mask2 & (1 << (j - 1)))) // esq tbm é branco
  20.                 return false;
  21.             if(i < m and (mask2 & (1 << (j + 1)))) // dir tbm é branco
  22.                 return false;
  23.             int cnt_black = 0;
  24.             for(int k = 0; k < 4; k++) {
  25.                 int x = dx[k] + i, y = dy[k] + j;
  26.                 if(x >= 0 and x < n and y >= 0 and y < m)
  27.                     cnt_black += black[x][y];
  28.             }
  29.             if(cnt_black == 0) return false;
  30.         }
  31.         return true;
  32.     }
  33. }
  34.  
  35. int count(int mask) {
  36.     int sz = 0;
  37.     while(mask) {
  38.         sz++;
  39.         mask -= mask&-mask;
  40.     }
  41.     return sz;
  42. }
  43.  
  44.  
  45. int dp[7][1 << 7];
  46. int seen[7][1 << 7];
  47.  
  48. int solve(int i, int mask1) {
  49.     if(i == n) return 0;
  50.     if(seen[i][mask1]) return dp[i][mask1];
  51.     int ans = 0;
  52.     for(int mask2 = 0; mask2 < (1 << m); mask2++) {
  53.         bool ok = true;
  54.         for(int j = 0; j < m; j++)
  55.             if(!check(i, j, mask1, mask2))
  56.                 ok = false;
  57.         if(ok) ans = max(ans, solve(i + 1, mask2) + count(mask2));
  58.     }
  59.     seen[i][mask1] = 1;
  60.     return dp[i][mask1] = ans;
  61. }
  62.  
  63. int32_t main() {
  64.  
  65.     ios_base::sync_with_stdio(false);
  66.     cin.tie(nullptr);
  67.  
  68.     cin >> n >> m >> p;
  69.  
  70.     for(int i = 0; i < p; i++) {
  71.         int x, y;
  72.         cin >> x >> y;
  73.         black[x - 1][y - 1] = 1;
  74.     }
  75.  
  76.     cout << solve(0, 0) << endl;
  77.  
  78.     return 0;
  79. }
Add Comment
Please, Sign In to add comment