You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1 KiB

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int dx[] = {-1, 0, 1, 0}; //上右下左
int dy[] = {0, 1, 0, -1}; //上右下左
struct Node
{
int x, y;
}b[N];
int bl;
char a[N][N];
int n,m;
int res;
void dfs(int u,int sum){
if(u==bl+1){
res = max(res, sum);
return;
}
dfs(u + 1, sum);
Node c[N];
int cl = 0;
int cx = b[u].x, cy = b[u].y;
for (int i = 0; i <= 4;i++){
int tx = dx[i] + cx, ty = dy[i] + cy;
if(tx<=0||tx>n||ty<=0||ty>m)continue;
if (a[tx][ty] == 'C' || a[tx][ty] == 'D' || a[tx][ty] == 'Z') return;
c[++cl] = {tx, ty};
}
a[cx][cy] = 'Z';
for (int i = 1; i <= cl; i++) a[c[i].x][c[i].y] += 2;
dfs(u + 1, sum + cl + 1);
a[cx][cy] = 'B';
for (int i = 1; i <= cl; i++) a[c[i].x][c[i].y] -= 2;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++){
cin >> a[i][j];
if (a[i][j] == 'B') b[++bl] = {i, j};
}
dfs(1, 0);
cout << res;
return 0;
}