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 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 OUTPUT (file castle.out)
5 9 16 4 1 E
题意:
给出 M ,N 代表有 M 列 N 行(1 <= M,N <= 50)。后给出 M 列 N 行个数,每个数表示东南西北四个方向墙之和。
1: wall to the west(西) ;2: wall to the north(北) ;4: wall to the east (东);8: wall to the south(南);
输出4行,分别表示:
1:总共的房间数 2:房间数最大值 3:打通一面墙后可达到的房间数最大值 4:打通的墙的方位表示
输出的最后一项要选择最西边的房间,若同时有多个,那么选择最南边的房间。选择的墙选 北 (即南邻的北墙)优先于 东(即西邻的东墙) 。
思路:
注意 M 和 N ,是先给出列数再给出行数的;
首先将数字划开,标记每个房间的东南西北是否有墙。DFS 将连通的房间分别标记,同时统计总数和最大连通数;
然后再从南向北,从西向东枚举墙数,判断北邻居 和 西邻居是否属于同一房间,并且判断是否大于当前最大值,记录 x,y,和墙的位置wall ;
最后按要求输出。
AC:
/* TASK:castle LANG:C++ ID:sum-g1 */ #include<stdio.h> #include<string.h> typedef struct { int n,w,e,s; }node; node no[55][55]; int n,m,map[55][55],vis[55][55],room_sum,room_num[3000]; int room_max_num = -1; int dir[4][2] = {-1,0,0,-1,1,0,0,1}; void dfs(int x,int y) { vis[x][y] = 1; map[x][y] = room_sum; room_num[room_sum]++; for(int i = 0;i < 4;i++) { int nx = x + dir[i][0]; int ny = y + dir[i][1]; if(nx <= n && ny <= m && nx >= 1 && ny >= 1 && !vis[nx][ny]) { if(x > 1 && i == 0 && !no[x][y].n && !no[nx][ny].s) dfs(nx,ny); if(x < n && i == 2 && !no[x][y].s && !no[nx][ny].n) dfs(nx,ny); if(y > 1 && i == 1 && !no[x][y].w && !no[nx][ny].e) dfs(nx,ny); if(y < m && i == 3 && !no[x][y].e && !no[nx][ny].w) dfs(nx,ny); } } } void init(int i,int j,int num) { if(num % 2) no[i][j].w = 1; if(num == 2 || num == 3 || num == 6 || num == 10 || num == 14 || num == 11 || num == 7 || num == 15 ) no[i][j].n = 1; if(num == 4 || num == 5 || num == 6 || num == 12 || num == 13 || num == 14 || num == 7 || num == 15 ) no[i][j].e = 1; if(num == 8 || num == 9 || num == 10 || num == 12 || num == 14 || num == 13 || num == 11 || num == 15 ) no[i][j].s = 1; } int main() { freopen("castle.in","r",stdin); freopen("castle.out","w",stdout); scanf("%d%d",&m,&n); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) { int num; scanf("%d",&num); init(i,j,num); } memset(vis,0,sizeof(vis)); memset(room_num,0,sizeof(room_num)); room_sum = 0; for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) { if(!vis[i][j]) { room_sum++; dfs(i,j); if(room_num[room_sum] > room_max_num) room_max_num = room_num[room_sum]; } } char wall; int max_change = 0,x,y; for(int j = 1;j <= m;j++) //从南向北 for(int i = n;i >= 1;i--) //从西向东 { int room_north = -1,room_east = -1; int room_in = map[i][j]; if(i > 1) room_north = map[i - 1][j]; if(j < m) room_east = map[i][j + 1]; if(room_north != -1 && no[i][j].n && room_in != room_north && room_num[room_in] + room_num[room_north] > max_change) { x = i; y = j; wall = 'N'; max_change = room_num[room_in] + room_num[room_north]; } //先考虑北 if(room_east != -1 && no[i][j].e && room_in != room_east && room_num[room_in] + room_num[room_east] > max_change) { x = i; y = j; wall = 'E'; max_change = room_num[room_in] + room_num[room_east]; } //再考虑东 } printf("%d\n%d\n",room_sum,room_max_num); printf("%d\n",max_change); printf("%d %d %c\n",x,y,wall); return 0; }