思路来自@Jackle ,但感觉自己的看起更好理解hh
C++ 代码
//新加入的牛只会影响它周围的牛
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
bool g[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int check(int x, int y){
int cnt = 0;
for (int i = 0; i < 4; i ++ ){
int tx = x + dx[i], ty = y + dy[i];
if(tx < 0 || ty < 0) continue;
if(g[tx][ty]) cnt++;
}
if(cnt == 3) {
return 1;
}else if(cnt == 4) {
return -1;
}
else return 0;
}
int main()
{
int n;
scanf("%d", &n);
int res = 0;
while (n -- ){
int x, y;
scanf("%d%d", &x, &y);
g[x][y] = true;
//检查自己
res += check(x, y);
//检查自己周围4个点
for (int i = 0; i < 4; i ++ ){
int tx = x + dx[i], ty = y + dy[i];
//格子上要有牛
if(!g[tx][ty] || tx < 0 || ty < 0) continue;
res += check(tx, ty);
}
printf("%d\n", res);
}
}