68 lines
1.5 KiB
C++
68 lines
1.5 KiB
C++
|
|
#include <bits/stdc++.h>
|
|||
|
|
using namespace std;
|
|||
|
|
const int N = 1010;
|
|||
|
|
int a[N][N];
|
|||
|
|
int ans;
|
|||
|
|
int n, m;
|
|||
|
|
/*
|
|||
|
|
3 5
|
|||
|
|
.....
|
|||
|
|
.#..#
|
|||
|
|
.....
|
|||
|
|
|
|||
|
|
答案:
|
|||
|
|
11
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
// 判断一个位置是不是荒地
|
|||
|
|
int check(int x, int y) {
|
|||
|
|
// 对于⼀块不存在杂物的荒地, 该荒地可以开垦当且仅当其上下左右四个⽅向相邻的格⼦均不存在杂物
|
|||
|
|
if (a[x][y] || a[x - 1][y] || a[x + 1][y] || a[x][y - 1] || a[x][y + 1])
|
|||
|
|
return false;
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 计算有多少个荒地
|
|||
|
|
int countHuangDi() {
|
|||
|
|
int sum = 0;
|
|||
|
|
for (int i = 1; i <= n; i++)
|
|||
|
|
for (int j = 1; j <= m; j++)
|
|||
|
|
if (check(i, j))
|
|||
|
|
sum++;
|
|||
|
|
return sum;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int main() {
|
|||
|
|
|
|||
|
|
cin >> n >> m;
|
|||
|
|
for (int i = 1; i <= n; i++)
|
|||
|
|
for (int j = 1; j <= m; j++) {
|
|||
|
|
char c;
|
|||
|
|
cin >> c;
|
|||
|
|
if (c == '#')
|
|||
|
|
a[i][j] = 1; // a[i][j]=1:杂物, a[i][j]=0:荒地
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 在任何一个杂物位置也不修改的情况下,计算一下荒地的个数
|
|||
|
|
for (int i = 1; i <= n; i++)
|
|||
|
|
for (int j = 1; j <= m; j++)
|
|||
|
|
if (check(i, j))
|
|||
|
|
ans++;
|
|||
|
|
|
|||
|
|
// 每个原来是杂物的位置 都有可能更改为荒地,然后计算在这样修改的情况下,荒地的个数
|
|||
|
|
for (int i = 1; i <= n; i++)
|
|||
|
|
for (int j = 1; j <= m; j++) {
|
|||
|
|
if (a[i][j]) {
|
|||
|
|
// 尝试把i,j位置改为荒地
|
|||
|
|
a[i][j] = 0;
|
|||
|
|
int sum = countHuangDi();
|
|||
|
|
ans = max(ans, sum);
|
|||
|
|
// 恢复现场
|
|||
|
|
a[i][j] = 1;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
cout << ans << endl;
|
|||
|
|
return 0;
|
|||
|
|
}
|