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行的区域,每一个面积单位四周墙壁是否有给出了(用的是二进制,比如12就是1100,就是西南方有墙,东北没有),问你这个区域有多少联通的面积,联通面积最大值多少,当去掉一面墙,此时最大的联通面积多大,去掉的是哪一堵墙(第几行第几列面积单位的哪一面墙)?(多个答案的时候,选择最靠西的单位,如果还多,选择最靠南的,还多,先选择北墙,再选择东墙)
思路:这道题目本身很简单,简单的种子染色,在处理图的时候用到了位运算的技巧,避免了重新建图,后来对墙的枚举顺序保证了多答案时按照题目要求,dxdy数组(移动数组)是提前计算好的,按照左上右下的顺序存储的,里面的逻辑拿纸模拟一遍就明白了附上演草纸,上面有一些的提示,有点凌乱
下面附上代码
1 /* 2 ID:fffgrdc1 3 PROB:castle 4 LANG:C++ 5 */ 6 #include<iostream> 7 #include<cstdio> 8 #include<cstring> 9 using namespace std; 10 11 int n,m; 12 int ini[52][52]; 13 int vis[52][52]={0},totcol=0,color[2510]={0}; 14 char chd[5]="WNES"; 15 int dx[4]={-1,0,1,0}; 16 int dy[4]={0,-1,0,1}; 17 void dfs(int y,int x) 18 { 19 for(int i=0,wall=1;i<4;wall<<=1,i++) 20 { 21 //printf("%d %d %d ",ini[y][x],wall,!(ini[y][x]&wall)); 22 if(!(ini[y][x]&wall)) 23 { 24 int tempx=dx[i]+x; 25 int tempy=dy[i]+y; 26 if((tempx>0&&tempy>0&&tempx<=m&&tempy<=n)&&!vis[tempy][tempx]) 27 { 28 vis[tempy][tempx]=totcol; 29 color[totcol]++; 30 dfs(tempy,tempx); 31 } 32 } 33 } 34 } 35 int main() 36 { 37 freopen("castle.in","r",stdin); 38 freopen("castle.out","w",stdout); 39 scanf("%d%d",&m,&n); 40 for(int i=1;i<=n;i++) 41 { 42 for(int j=1;j<=m;j++) 43 { 44 scanf("%d",&ini[i][j]); 45 } 46 } 47 memset(color,0,sizeof(color)); 48 for(int i=1;i<=n;i++) 49 { 50 for(int j=1;j<=m;j++) 51 { 52 if(!vis[i][j]) 53 { 54 totcol++; 55 color[totcol]++; 56 vis[i][j]=totcol; 57 dfs(i,j); 58 } 59 } 60 } 61 int inians=0; 62 int aimx,aimy,ans=0,aimd; 63 /* 64 for(int i=1;i<=n;i++) 65 { 66 for(int j=1;j<=m;j++) 67 { 68 printf("%2d ",vis[i][j]); 69 } 70 printf(" "); 71 } 72 for(int i=1;i<=totcol;i++) 73 { 74 printf("%d ",color[i]); 75 }printf(" "); 76 */ 77 for(int j=m;j>0;j--) 78 { 79 for(int i=1;i<=n;i++) 80 { 81 for(int k=4;k>0;k--) 82 { 83 int tempk=k==4?0:k; 84 int tempx=dx[tempk]+j; 85 int tempy=dy[tempk]+i; 86 if((tempx>0&&tempy>0&&tempx<=m&&tempy<=n)&&vis[i][j]!=vis[tempy][tempx]) 87 { 88 //printf("%d %d ",color[vis[i][j]],color[vis[tempy][tempx]]); 89 if(ans<=(color[vis[i][j]]+color[vis[tempy][tempx]])) 90 { 91 ans=color[vis[i][j]]+color[vis[tempy][tempx]]; 92 //printf("%d %d %d ",color[vis[i][j]],color[vis[tempy][tempx]],ans); 93 aimd=tempk; 94 aimx=j; 95 aimy=i; 96 } 97 } 98 } 99 } 100 } 101 for(int i=1;i<=totcol;i++) 102 { 103 inians=(inians<color[i])?color[i]:inians; 104 } 105 printf("%d %d %d %d %d %c ",totcol,inians,ans,aimy,aimx,chd[aimd]); 106 return 0; 107 }