King Jack wants to prevent enemy pieces from landing on his land. To accomplish this, he will place pieces on as few squares as possible such that every square is attacked, and so any invader will be taken. His piece of choice is the Rook. He already placed a few rooks, but now attacks are becoming more frequent, and he must place more.
His land consists of a grid of squares, and he currently has rooks already deployed in positions (not necessarily distinct) onto the board. The positions are pairs of integers, with . Your task is to find the last three digits of the minimum number of additional rooks Jack needs such that every square on his land is attacked by at least one rook.
Sample input (on a grid with 4 rooks already placed)
1 3
2 2
2 4
4 3
Input explanation
The rooks are placed on squares , , , and .
Answer and explanation
Only rook is needed, which will be placed at .
Note that the actual input has 10000 positions.
Details and assumptions
You may find the list of positions here . Each of the 10000 lines contains a space-separated pair of integers that corresponds to the position of a single rook.
A rook attacks every square in the same row as it and/or the same column as it, and so a square with a rook is considered to be attacked.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Python Solution: