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
题意:输入一个地图,统计里面有多少连通的房间房间,最大的连通数,如果拆一道墙之后最大的连通数是多少并且输出所拆的房间矩阵的编号和拆的那面墙。规定拆的房间满足时最西边的然后满足最南边的,对于每间房间首先考虑拆西边,然后再拆南边,再然后拆北边,最后考虑拆东边。1表示西边又墙,2表示北边有墙,4表示东边有墙,8表示南边有墙。每个房间的有几面墙就是这些数字之和。
分析:使用二进制运算
第一步:使用dfs标记整个地图 flag[][] 属于哪个连通图;
第二步:统计每个连通图的大小存入 size[];
第三步:枚举删墙。
心得:这个题目搞了一个上午了,就是这个输出的方向没搞明白
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /* 2 ID: dizzy_l1 3 LANG: C++ 4 TASK: castle 5 */ 6 #include<iostream> 7 #include<cstring> 8 #include<cstdio> 9 #define MAXN 60 10 11 using namespace std; 12 13 bool visited[MAXN][MAXN]; 14 int map[MAXN][MAXN],flag[MAXN][MAXN],size[MAXN*MAXN]; 15 int n,m,cnt; 16 17 void dfs_flag(int i,int j) 18 { 19 flag[i][j]=cnt; 20 visited[i][j]=true; 21 if(j>1&&!visited[i][j-1]&&(map[i][j]&1)) 22 dfs_flag(i,j-1); 23 if(i<n&&!visited[i+1][j]&&(map[i][j]&8)) 24 dfs_flag(i+1,j); 25 if(j<m&&!visited[i][j+1]&&(map[i][j]&4)) 26 dfs_flag(i,j+1); 27 if(i>1&&!visited[i-1][j]&&(map[i][j]&2)) 28 dfs_flag(i-1,j); 29 } 30 31 int count_size() 32 { 33 int i,j,ans; 34 memset(size,0,sizeof(size)); 35 for(i=1; i<=n; i++) 36 for(j=1; j<=m; j++) 37 size[flag[i][j]]++; 38 ans=0; 39 for(i=1; i<=cnt; i++) 40 if(ans<size[i]) 41 ans=size[i]; 42 return ans; 43 } 44 45 int ans_cnt,ans_max1,ans_max2,ans_i,ans_j; 46 char ans_p; 47 48 void work_ans() 49 { 50 int i,j; 51 ans_max2=0; 52 for(j=1; j<=m; j++) 53 { 54 for(i=n; i>=1; i--) 55 { 56 int t; 57 map[i][j]^=15; 58 if(j>1&&(map[i][j]&1)) 59 { 60 if(flag[i][j-1]!=flag[i][j]) 61 t=size[flag[i][j-1]]+size[flag[i][j]]; 62 else 63 t=size[flag[i][j]]; 64 if(ans_max2<t) 65 { 66 ans_max2=t; 67 ans_i=i; 68 ans_j=j; 69 ans_p='W'; 70 } 71 } 72 if(i<n&&(map[i][j]&8)) 73 { 74 if(flag[i+1][j]!=flag[i][j]) 75 t=size[flag[i+1][j]]+size[flag[i][j]]; 76 else 77 t=size[flag[i][j]]; 78 if(ans_max2<t) 79 { 80 ans_max2=t; 81 ans_i=i; 82 ans_j=j; 83 ans_p='S'; 84 } 85 } 86 if(i>1&&(map[i][j]&2)) 87 { 88 if(flag[i-1][j]!=flag[i][j]) 89 t=size[flag[i-1][j]]+size[flag[i][j]]; 90 else 91 t=size[flag[i][j]]; 92 if(ans_max2<t) 93 { 94 ans_max2=t; 95 ans_i=i; 96 ans_j=j; 97 ans_p='N'; 98 } 99 } 100 if(j<m&&(map[i][j]&4)) 101 { 102 if(flag[i][j+1]!=flag[i][j]) 103 t=size[flag[i][j+1]]+size[flag[i][j]]; 104 else 105 t=size[flag[i][j]]; 106 if(ans_max2<t) 107 { 108 ans_max2=t; 109 ans_i=i; 110 ans_j=j; 111 ans_p='E'; 112 } 113 } 114 } 115 } 116 } 117 118 int main() 119 { 120 freopen("castle.in","r",stdin); 121 freopen("castle.out","w",stdout); 122 int i,j; 123 while(scanf("%d %d",&m,&n)==2) 124 { 125 for(i=1; i<=n; i++) 126 for(j=1; j<=m; j++) 127 scanf("%d",&map[i][j]),map[i][j]^=15; 128 memset(flag,0,sizeof(flag)); 129 memset(visited,false,sizeof(visited)); 130 cnt=0; 131 for(i=1; i<=n; i++) 132 for(j=1; j<=m; j++) 133 if(!visited[i][j]) 134 { 135 cnt++; 136 dfs_flag(i,j); 137 } 138 ans_cnt=cnt; 139 ans_max1=count_size(); 140 work_ans(); 141 printf("%d\n",ans_cnt); 142 printf("%d\n%d\n",ans_max1,ans_max2); 143 printf("%d %d %c\n",ans_i,ans_j,ans_p); 144 } 145 return 0; 146 }