AcWing 3371. 舒适的奶牛 简洁清晰
原题链接
简单
作者:
蓝泽
,
2022-05-10 12:56:17
,
所有人可见
,
阅读 246
#include <iostream>
using namespace std;
const int N = 1010;
int n, g[N][N];
bool st[N][N]; //记录舒适的奶牛位置
bool judge(int x, int y)
{
if (g[x - 1][y] + g[x][y + 1] + g[x + 1][y] + g[x][y - 1] == 3) return 1;
else return 0;
}
int main()
{
cin >> n;
int cnt = 0;
for (int i = 0; i < n; i ++)
{
int x, y;
cin >> x >> y;
x ++, y ++;
g[x][y] = 1;
if (judge(x, y)) //判断加入的奶牛是否舒适
{
st[x][y] = 1;
cnt ++;
}
// 判断加入的奶牛后对周围四个奶牛影响
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
for (int j = 0; j < 4; j ++)
{
int a = x + dx[j], b = y + dy[j];
if(!g[a][b]) continue; //如果周围没有奶牛
if (st[a][b] == 1) //舒适->不舒适
{
st[a][b] = 0;
cnt --;
}
else if (judge(a, b)) //不舒适->舒适
{
st[a][b] = 1;
cnt ++;
}
}
cout << cnt << endl;
}
return 0;
}