## Comfortable Cow (February 2021 Bronze, Problem 2)

http://www.usaco.org/index.php?page=viewproblem2&cpid=1108

## Question

I was comparing my code to the solution but couldn’t figure out why they produced different answers. Could anyone help me figure out why?

It works for the first test case but fails all the others.

## My Work

```
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int ans = 0;
int main() {
int N;
cin >> N;
map<pair<int, int>, int> mp;
for (int i = 0; i < N; i++) {
int x, y;
cin >> x >> y;
for (int d = 0; d < 4; d++) {
mp[{x + dx[d], y + dy[d]}]++;
if (mp[{x + dx[d], y + dy[d]}] == 3) ans++;
if (mp[{x + dx[d], y + dy[d]}] >= 4) ans--;
}
if (mp[{x, y}] == 3) ans++;
if (mp[{x, y}] >= 4) ans--;
cout << ans << "\n";
}
}
```

In my code, I’m using a map to track the number of neighbors rather than checking it each time.

Here is the solution code: Contest Results