The Castle
IOI'94 - Day 1
In a stroke of luck almost beyond imagination, Farmer John was sent a ticket to the Irish Sweepstakes (really a lottery) for his birthday. This ticket turned out to have only the winning number for the lottery! Farmer John won a fabulous castle in the Irish countryside.
Bragging rights being what they are in Wisconsin, Farmer John wished to tell his cows all about the castle. He wanted to know how many rooms it has and how big the largest room was. In fact, he wants to take out a single wall to make an even bigger room.
Your task is to help Farmer John know the exact room count and sizes.
The castle floorplan is divided into M (wide) by N (1 <=M,N<=50) square modules. Each such module can have between zero and four walls. Castles always have walls on their "outer edges" to keep out the wind and rain.
Consider this annotated floorplan of a castle:
1 2 3 4 5 6 7
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # -># | | | | # #
#############################
# = Wall -,| = No wall
-> = Points to the wall to remove to
make the largest possible new room
By way of example, this castle sits on a 7 x 4 base. A "room" includes any set of connected "squares" in the floor plan. This floorplan contains five rooms (whose sizes are 9, 7, 3, 1, and 8 in no particular order).
Removing the wall marked by the arrow merges a pair of rooms to make the largest possible room that can be made by removing a single wall.
The castle always has at least two rooms and always has a wall that can be removed.
PROGRAM NAME: castle
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.
Each module number tells how many 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
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.
| Line 1: |
Two space-separated integers: M and N |
| Line 2..: |
M x N integers, several per line. |
SAMPLE INPUT (file castle.in)
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
OUTPUT FORMAT
The output contains several 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 wall farthest to the west (and then, if still tied, farthest to the south). 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 OUTPUT (file castle.out)
5
9
16
4 1 E
題目意思:
給出一個(gè)n*m方塊組成的城堡,每個(gè)方塊四周都有圍墻情況,可以構(gòu)成一個(gè)個(gè)獨(dú)立的房間。對每個(gè)房間找方塊數(shù)最多的。并要求拆了一面墻,使被拆的墻所連接的兩個(gè)房間的方塊和是最大的。
然后輸出被拆那面墻的位置及方向。
代碼如下:

/**//*
LANG: C
TASK: castle
*/
#include<stdio.h>
#define MAX 55
struct queue


{
int x, y;
}queue[MAX * MAX];
int graph[MAX][MAX][4], map[MAX][MAX], sum[MAX];

int M[4][2] =
{
{-1, 0},
{0, 1},
{1, 0},
{0, -1}};
void Set(int x, int y, int t)//存取每個(gè)單位方塊的圍墻情況


{
graph[x][y][2] = t / 8, t %= 8;
graph[x][y][1] = t / 4, t %= 4;
graph[x][y][0] = t / 2, t %= 2;
graph[x][y][3] = t;
map[x][y] = 0;
}
int Bfs(int n, int m)


{
int i, j, k, number = 1, tial, head, row, col;
for (i = 0; i < n; i++)

{
for (j = 0; j < m; j++)

{
if (!map[i][j])

{
head = tial = 0;
map[i][j] = number;//標(biāo)記該房間的房間號。
queue[tial].x = i;//對沒被遍歷過的方塊入棧
queue[tial++].y = j;
for (head = 0; head < tial; head++)

{
for (k = 0; k < 4; k++)

{
row = queue[head].x + M[k][0];
col = queue[head].y + M[k][1];
if (row >= 0 && row < n && col >= 0 && col < m && !map[row][col] && !graph[row][col][(k+2) % 4])

{
queue[tial].x = row;
queue[tial++].y = col;
map[row][col] = number;
}
}
}
sum[number] = tial;//記錄該房間的方塊數(shù)
number++;
}
}
}
return number - 1;
}
int main()


{
char direction;
int i, j, n, m, temp, numberOfroom, max, row, col;
FILE * fin = fopen("castle.in", "r");
FILE * fout = fopen("castle.out", "w");
fscanf(fin, "%d%d", &m, &n);
for (i = 0; i < n; i++)

{
for (j = 0; j < m; j++)

{
fscanf(fin, "%d", &temp);
Set(i, j, temp);
}
}
numberOfroom = Bfs(n, m);
fprintf(fout, "%d\n", numberOfroom);
max = 0;
for (i = 1; i <= numberOfroom; i++)//求方塊最大值

{
if (max < sum[i])

{
max = sum[i];
}
}
fprintf(fout, "%d\n", max);
max = 0;
for (j = 0; j < m; j++)//移出的那個(gè)方塊要最靠西,不行就選最靠南的

{
for (i = n - 1; i >= 0; i--)

{
if (i - 1 >= 0 && map[i][j] != map[i-1][j] && max < sum[map[i][j]] + sum[map[i-1][j]])

{
max = sum[map[i][j]] + sum[map[i-1][j]];
row = i + 1;
col = j + 1;
direction = 'N';
}
if (j + 1 < m && map[i][j] != map[i][j+1] && max < sum[map[i][j]] + sum[map[i][j+1]])

{
max = sum[map[i][j]] + sum[map[i][j+1]];
row = i + 1;
col = j + 1;
direction = 'E';
}
}
}
fprintf(fout, "%d\n%d %d %c\n", max, row, col, direction);
//system("pause");
return 0;
}

/**//*
3
9
17
4 1 N

3
9
18
3_1_N

5 5
3 2 6 3 6
1 8 4 1 4
13 7 13 9 4
3 0 2 6 5
9 8 8 12 13
*/
