IOI '94 - Haninge, Sweden

The Castle

     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 N×M (1 ≤ N, M ≤ 50) 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 Format

The map is stored in the form of numbers, one number for each module, M numbers on each of N 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 M and N.
  • The following N lines each contains M integers from 0 to 15 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:

  • 1: wall to the west
  • 2: wall to the north
  • 4: wall to the east
  • 8: wall to the south

Output Format

The output should contain 4 lines.

  • Line 1: The number of rooms the castle has
  • Line 2: The size of the largest room
  • Line 3: The size of the largest room creatable by removing one wall
  • Line 4: 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

All Submissions
Best Solutions


Point Value: 10
Time Limit: 2.00s
Memory Limit: 8M
Added: Dec 25, 2013

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

Is it the official IOI Problem? It seems that it is asking for more things... and also giving more restrictions.... It doesn't have special checker...

We have indeed slightly modified the problem to restrict the possible outputs -- this makes it much easier to judge, but should not affect the problem difficulty at all. Please be aware of the additional stipulation we have added in the last paragraph of the Output Format section.