Alex is an animal rights activist out to protect monkeys from the evil corporations that use them to test shampoo. He is planning a daring breakout of a group of monkeys contained in a room in a secret underground lab. The monkeys are contained in a system of cages that border each other, all without doors. Some cages have monkeys, and some are empty. A cage is a set of adjacent squares that is enclosed by wall squares. Alex wants to know how many cages have monkeys in them so that he knows how many holes he'll need to drill in the ceiling to rappel down from. On the floor plan, a wall is denoted by a #
, an empty space is denoted by a .
and a monkey is denoted by an M
. It is guaranteed that the edges of the room will consist entirely of walls.
Input Specification
Two space-separated integers and , denoting the height and width of the room, respectively.
lines of length each, denoting the layout of the floor.
Output Specification
The number of cages that contain monkeys.
Sample Input
9 10
##########
##M#.M.#.#
#MM####..#
#M#.#.#..#
##..#.#.M#
#..##..#M#
##M#######
#.#MM#.M.#
##########
Sample Output
6
Comments
Is only:
considered a room, or
as well?
If the diagonals are also considered (8 directions in total), then the answer to the sample input would be
3
.Not sure what you mean as the second test case you've given wouldn't be valid. A room is a strongly connected component of room nodes (one node per character) when there exist edges connecting each node with its left, upper, lower, or right neighbour if said neighbour exists.
This comment is hidden due to too much negative feedback. Show it anyway.
I didn't write this problem. I was just trying to help.