## Alex and Animal Rights

View as PDF

Points: 7 (partial)
Time limit: 0.6s
Memory limit: 256M

Authors:
Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

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

• commented on Dec. 13, 2017, 4:42 p.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on Jan. 7, 2016, 1:04 p.m. edited

Is only:

###
#.#
###

considered a room, or

 #
#. #
#

as well?

• commented on Jan. 11, 2016, 4:17 p.m.

If the diagonals are also considered (8 directions in total), then the answer to the sample input would be 3.

• commented on Jan. 7, 2016, 4:52 p.m.

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.

• commented on Jan. 7, 2016, 4:53 p.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on Jan. 7, 2016, 7:36 p.m.

I didn't write this problem. I was just trying to help.