Do use hexagon grid

AtCoder
IDabc269_d
Time2000ms
Memory256MB
Difficulty
We have an infinite hexagonal grid shown below. Initially, all squares are white. ![image](https://img.atcoder.jp/abc269/b61b1e0469588c61352a7fa7f7865351.png) A hexagonal cell is represented as $(i,j)$ with two integers $i$ and $j$. Cell $(i,j)$ is adjacent to the following six cells: * $(i-1,j-1)$ * $(i-1,j)$ * $(i,j-1)$ * $(i,j+1)$ * $(i+1,j)$ * $(i+1,j+1)$ Takahashi has painted $N$ cells $(X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N)$ black. Find the number of connected components formed by the black cells. Two black cells belong to the same connected component when one can travel between those two black cells by repeatedly moving to an adjacent black cell. ## Constraints * All values in the input are integers. * $1 \le N \le 1000$ * $|X_i|,|Y_i| \le 1000$ * The pairs $(X_i,Y_i)$ are distinct. ## Input The input is given from Standard Input in the following format: $N$ $X_1$ $Y_1$ $X_2$ $Y_2$ $\vdots$ $X_N$ $Y_N$ [samples]
Samples
Input #1
6
-1 -1
0 1
0 2
1 0
1 2
2 0
Output #1
3

After Takahashi paints cells black, the grid looks as shown below.
![image](https://img.atcoder.jp/abc269/865747dac44d93b150ecbed462ac4ef3.png)
The black squares form the following three connected components:

*   $(-1,-1)$
*   $(1,0),(2,0)$
*   $(0,1),(0,2),(1,2)$
Input #2
4
5 0
4 1
-3 -4
-2 -5
Output #2
4
Input #3
5
2 1
2 -1
1 0
3 1
1 -1
Output #3
1
API Response (JSON)
{
  "problem": {
    "name": "Do use hexagon grid",
    "description": {
      "content": "We have an infinite hexagonal grid shown below. Initially, all squares are white. ![image](https://img.atcoder.jp/abc269/b61b1e0469588c61352a7fa7f7865351.png) A hexagonal cell is represented as $(i,j)",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc269_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "We have an infinite hexagonal grid shown below. Initially, all squares are white.\n![image](https://img.atcoder.jp/abc269/b61b1e0469588c61352a7fa7f7865351.png)\nA hexagonal cell is represented as $(i,j)...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments