2013
11-09

# The Castle

     1   2   3   4   5   6   7
#############################
1 #   |   #   |   #   |   |   #
#####---#####---#---#####---#
2 #   #   |   #   #   #   #   #
#---#####---#####---#####---#
3 #   |   |   #   #   #   #   #
#---#########---#####---#---#
4 #   #   |   |   |   |   #   #
#############################
(Figure 1)

#  = Wall
|  = No wall
-  = No wall

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

The castle is divided into m * n (m<=50, n<=50) square modules. Each such module can have between zero and four walls.

Your program is to read from standard input. The first line contains the number of modules in the north-south direction and the number of modules in the east-west direction. In the following lines each module is described by a number (0 <= p <= 15). This number is the sum of: 1 (= wall to the west), 2 (= wall to the north), 4 (= wall to the east), 8 (= wall to the south). Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1. The castle always has at least two rooms.

Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).

4
7
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

5
9

//* @author
import java.io.*;
import java.util.*;

public class Main
{
static Room[][] matrix;
static int row;
static int col;
public static void main(String[] args) throws Exception
{
for(int i=0; i< row; i++)
{
for(int j=0; j< col; j++)
{
if(!matrix[i][j].bFlag)
{
process(i,j);
sumCount++;
if(maxCount>INF)
{
INF = maxCount;
}
maxCount=0;
}
}
}
System.out.print(sumCount);
System.out.print("\n"+INF);
}

public static void readFile() throws Exception
{
StringTokenizer st = null;
matrix = new Room[row][col];
int count = 0;
while(count< row)
{
int j = 0;
Room room = null;
while(st.hasMoreTokens())
{
room = new Room(Integer.valueOf(st.nextToken()));
matrix[count][j] = room;
j++;
}
count++;
}
}

static void displayRoom()
{
for(int i=0; i< row; i++)
{
for(int j=0; j< col; j++)
System.out.println(matrix[i][j]);
System.out.println();
}
}

static int sumCount = 0;
static int maxCount = 0;
static int INF = 0;
static void process(int i,int j)
{
if(i< 0 || j< 0 || i>=row || j>=col)
return;
if(matrix[i][j].bFlag)
return;
maxCount++;
matrix[i][j].bFlag = true;
if(matrix[i][j].states[0][1]==2)
{
process(i-1,j);
}
if(matrix[i][j].states[1][2]==2)
{
process(i,j+1);
}
if(matrix[i][j].states[2][1]==2)
{
process(i+1,j);
}
if(matrix[i][j].states[1][0]==2)
{
process(i,j-1);
}

}

static class Room
{
int[][] states = new int[3][3];
boolean bFlag = false;
Room(int flag)
{
states[1][1] = 1;
switch(flag)
{
case 0:
states[0][1] = 2;
states[1][2] = 2;
states[1][0] = 2;
states[2][1] = 2;
break;
case 1:
states[0][1] = 2;
states[1][2] = 2;
states[2][1] = 2;
break;
case 2:
states[1][0] = 2;
states[1][2] = 2;
states[2][1] = 2;
break;
case 3:
states[1][2] = 2;
states[2][1] = 2;
break;
case 4:
states[0][1] = 2;
states[1][0] = 2;
states[2][1] = 2;
break;
case 5:
states[0][1] = 2;
states[2][1] = 2;
break;
case 6:
states[1][0] = 2;
states[2][1] = 2;
break;
case 7:
states[2][1] = 2;
break;
case 8:
states[0][1] = 2;
states[1][0] = 2;
states[1][2] = 2;
break;
case 9:
states[0][1] = 2;
states[1][2] = 2;
break;
case 10:
states[1][0] = 2;
states[1][2] = 2;
break;
case 11:
states[1][2] = 2;
break;
case 12:
states[0][1] = 2;
states[1][0] = 2;
break;
case 13:
states[0][1] = 2;
break;
case 14:
states[1][0] = 2;
break;
case 15:
break;
}
}

public String toString()
{
for(int i=0; i< 3; i++)
{
for(int j=0; j< 3; j++)
System.out.print(states[i][j] + " ");
System.out.println();
}
return "";
}
}
}