Cube Dividing
Time limit5sMemory limit512 MB
Count the face-connected components of unit cubes left in an A x B x C box after removing N given cubes, where the box is huge but N is at most 20000.
Problem
Pablo Cubarson is a cubism artist. His new piece starts from a cuboid made of unit cubes. He plans to carve the shape he wants by removing of those unit cubes. Just before starting, he noticed that the removal can split the remaining material into several parts that no longer touch each other. A piece that falls apart into several parts goes against his sense of beauty, so he wants to know how many parts his plan produces.
Count the connected components formed by the unit cubes that remain after the cubes are removed. Two cubes are connected when they share a face.
Input
The input is a single test case in the following format.
A B C N
X1 Y1 Z1
...
XN YN ZN
The first line contains the integers , , and . , and () give the size of the cuboid: it is units wide from left to right, units tall from bottom to top, and units deep from front to back. (, ) is the number of cubes that are removed.
Each of the next lines contains the integers (), () and (). They mean that the cube steps from the left, steps from the bottom and steps from the front is removed. Coordinates count from 0. All given positions are distinct.
Output
Print the number of connected components that remain after the listed cubes are removed.