72 lines
1.6 KiB
C++
72 lines
1.6 KiB
C++
|
|
#include <bits/stdc++.h>
|
|||
|
|
using namespace std;
|
|||
|
|
const int N = 1010;
|
|||
|
|
typedef pair<int, int> PII;
|
|||
|
|
vector<PII> vec;
|
|||
|
|
int a[N][N];
|
|||
|
|
int ans;
|
|||
|
|
int n, m;
|
|||
|
|
|
|||
|
|
int mx; // 最多带来几个荒地的价值
|
|||
|
|
/*
|
|||
|
|
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 main() {
|
|||
|
|
freopen("T1.in", "r", stdin);
|
|||
|
|
|
|||
|
|
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:荒地
|
|||
|
|
vec.push_back({i, j});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 在任何一个杂物位置也不修改的情况下,计算一下荒地的个数
|
|||
|
|
for (int i = 1; i <= n; i++)
|
|||
|
|
for (int j = 1; j <= m; j++)
|
|||
|
|
if (check(i, j))
|
|||
|
|
ans++;
|
|||
|
|
|
|||
|
|
// 每个原来是杂物的位置 都有可能更改为荒地,然后计算在这样修改的情况下,荒地的个数
|
|||
|
|
for (auto p : vec) {
|
|||
|
|
int i = p.first, j = p.second;
|
|||
|
|
// 尝试把i,j位置改为荒地
|
|||
|
|
a[i][j] = 0;
|
|||
|
|
|
|||
|
|
int sum = check(i, j);
|
|||
|
|
if (i - 1 >= 1)
|
|||
|
|
sum += check(i - 1, j);
|
|||
|
|
if (i + 1 <= n)
|
|||
|
|
sum += check(i + 1, j);
|
|||
|
|
if (j - 1 >= 1)
|
|||
|
|
sum += check(i, j - 1);
|
|||
|
|
if (j + 1 <= m)
|
|||
|
|
sum += check(i, j + 1);
|
|||
|
|
|
|||
|
|
mx = max(mx, sum);
|
|||
|
|
// 恢复现场
|
|||
|
|
a[i][j] = 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
cout << ans + mx << endl;
|
|||
|
|
return 0;
|
|||
|
|
}
|