Language: C++

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int H, W;
char board[15][15];
short dp[2][180000][122];

void chmax(int &A, int B) {
    A = max(A, B);
}

int B;
int merge(int A, int i) {
    while(A >= B) A -= B;
    
    int b = A % 3;
    A = A - (A % 3);
    if(b == 1 && i != 0) b = 2;
    A = (A + b) * 3 + i;
    return A;
}
int get_head(int A) {
    return A / B;
}
int get_tail(int A) {
    return A % 3;
}
void thr_eval(int val, int &cnt_black, int &cnt_white, int &cnt_adj_b) {
    if(val == 0) {
        cnt_white++;
    }
    if(val == 1) {
        cnt_black++;
    }
    if(val == 2) {
        cnt_black++;
        cnt_adj_b++;
    }
}

int main() {
    scanf("%d%d", &H, &W);
    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            scanf(" %c", &board[i][j]);
        }
    }

    int P = H*W, Q = H*W;
    B = 1;
    for(int i=0; i<W-1; i++) B *= 3;

    memset(dp, -1, sizeof(dp));
    dp[0][0][0] = 0;
    for(int pos=0; pos<P; pos++) {
        int row = pos / W, col = pos % W;
        int cur = pos % 2, nxt = 1 - cur;
        for(int bit=0; bit<(3*B); bit++) {
            for(int diff=0; diff<Q; diff++) {
                if(dp[cur][bit][diff] < 0) continue;

                int val = dp[cur][bit][diff];

                int cnt_black = 0, cnt_white = 0, cnt_adj_b = 0;
                // 真上
                if(row != 0) {
                    int val = get_head(bit);
                    thr_eval(val, cnt_black, cnt_white, cnt_adj_b);
                }
                // 左隣
                if(col != 0) {
                    int val = get_tail(bit);
                    thr_eval(val, cnt_black, cnt_white, cnt_adj_b);
                }

                char c = board[row][col];
                if(c == '#' || c == '?') {
                    int n_diff = diff + 1 - cnt_black;
                    int n_self = (cnt_black ? 2 : 1);
                    if(n_diff > 0) {
                        int nbit = merge(bit, n_self);
                        chmax(dp[nxt][nbit][n_diff], val + 1);
                    }
                }
                if(c == '.' || c == '?') {
                    if(row != 0) {
                        if(get_head(bit) == 1) continue;
                    }
                    if(row == W-1) {
                        if(get_tail(bit) == 1) continue;
                    }

                    if(cnt_white == 0) {
                        int nbit = merge(bit, 0);
                        chmax(dp[nxt][nbit][diff], val);
                    }
                }

                dp[cur][bit][diff] = -1;
            }
        }
    }

    int ans = -1;
    for(int bit=0; bit<3*B; bit++) {
        if(dp[P%2][bit][1] < 0) continue;
        chmax(ans, dp[P%2][bit][1]);
    }
    printf("%d\n", ans);
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:84:51: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'short int'
   84 |                         chmax(dp[nxt][nbit][n_diff], val + 1);
      |                               ~~~~~~~~~~~~~~~~~~~~^
a.cc:10:17: note:   initializing argument 1 of 'void chmax(int&, int)'
   10 | void chmax(int &A, int B) {
      |            ~~~~~^
a.cc:97:49: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'short int'
   97 |                         chmax(dp[nxt][nbit][diff], val);
      |                               ~~~~~~~~~~~~~~~~~~^
a.cc:10:17: note:   initializing argument 1 of 'void chmax(int&, int)'
   10 | void chmax(int &A, int B) {
      |            ~~~~~^