You are on page 1of 2

You are presented with a map of a kingdom.

Empty land on this map is depicted as


. (without the quotes), and mountains are depicted by #. This kingdom has
factions whose armies are represented by lowercase letters in the map. Two armies
of the same letter belong to the same faction.

Armies can go up, down, left, and right, but cannot travel through mountains. A
region is an area enclosed by mountains. From this it can be deduced that armies
cannot travel between different regions. A region is said to be controlled by a
faction if its the only faction with an army in that region. If there are at least
two armies from different factions in a region, then that region is said to be
contested. Your task is to determine how many regions each faction controls and how
many contested regions there are.

Input:

The first line is the number of test cases T. Each test case will have two numbers
N and M, each on their own line given in that order. Following that is N lines of M
characters that represent the map.

Output:

For each test case, output one line of the form Case C: (without the quotes),
where C is the case number (starting from 1). On the following lines, output the
factions that appear in the grid in alphabetical order if the faction controls at
least one region and how many regions that letter controls. Factions that dont
control any regions should not be in the output. After this, print one last line of
the form contested K where K is the number of regions that contain at least two
distinct letters.

See the sample output below for details.

Constraints:

1 T 100
1 N 100
1 M 100

Sample Input:

2
2
2
.#
#a
12
15
###########....
#.......c.###..
####......#.#..
.#.########.#..
##...#..b#..#..
#.a.#...#...###
####.#.#d###..#
......#...e#xx#
.#....#########
.#.x..#..#.....
.######.c#.....
......####.....
Sample Output:
Case 1:
a 1
Case 2:
a 1
b 1
c 2
x 2
contested 1

You might also like