## IOI '94 P2 - The Castle

View as PDF

Points: 10
Time limit: 1.0s
Memory limit: 8M

Problem type
##### IOI '94 - Haninge, Sweden
     1   2   3   4   5   6   7
#############################
1 #   |   #   |   #   |   |   #
#####---#####---#---#####---#
2 #   #   |   #   #   #   #   #
#---#####---#####---#####---#
3 #   |   |   #   #   #   #   #
#---#########---#####---#---#
4 # ->#   |   |   |   |   #   #
#############################  (Figure 1)

#  = Wall         -,|  = No wall
-> = It points to the wall to remove to
make the largest possible new room

Figure 1 shows the map of a castle. Write a program that calculates:

1. How many rooms the castle has,
2. How big the largest room is,
3. The size of the largest room creatable by removing one wall, and
4. Which wall to remove from the castle to make as large a room as possible.

The castle is divided into square modules. Each such module can have between zero and four walls. The castle always has at least two rooms and a wall that can be removed.

#### Input Specification

The map is stored in the form of numbers, one number for each module, numbers on each of lines to describe the floorplan. The input order corresponds to the numbering in the example diagram above.

• The input starts with two space separated integers and .
• The following lines each contains integers from to that each describe a single module of the castle.

Each module number indicates which of the four walls exist, and is the sum of up to four integers:

• : wall to the west
• : wall to the north
• : wall to the east
• : wall to the south

#### Output Specification

The output should contain lines.

• Line : The number of rooms the castle has
• Line : The size of the largest room
• Line : The size of the largest room creatable by removing one wall
• Line : The single wall to remove to make the largest room possible

Choose the optimal wall to remove from the set of optimal walls by choosing the module farthest to the west (and then, if still tied, farthest to the south). If still tied, choose N before E. Name that wall by naming the module that borders it on either the west or south, along with a direction of N or E giving the location of the wall with respect to the module.

#### Sample Input

7 4
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13

#### Sample Output

5
9
16
4 1 E