Description
Contestants often wonder what jury members do during a contest. Someone thinks that they spend all the contest fixing bugs in tests. Others say that the jury members watch the contest excitedly and even bet on who will win. However, in reality, the jury members prefer to play computer games, giving a complete control of the contest to heartless machines.
Another Ball Killer is one of the favorite games of the jury. Its rules are very simple:
- The game is played by one person on a rectangular field of size n × m. At the initial moment, each cell of the field contains a ball of one of five colors: blue, green, red, white, or yellow.
- At each move, the player chooses some figure (a connected group of two or more balls of the same color; balls are called connected if their cells have a common side) and removes it from the field. After that the balls that were above the removed balls fall down. If a column without the balls appears, then all the columns on its right are shifted to the left.
The image below shows how the field changes after the removal of the largest figure.
The player is awarded k × (k − 1) points for his move, where k is the size of the removed figure, i.e. the number of balls in it. - The game is finished when there are no figures left on the field. The goal is to get as many points as possible by the end of the game.
Lazy jury members play Another Ball Killer using the following algorithm:
01 Choose the color of one of the balls in the field as the main color. 02 While there is at least one figure: 03 While there is at least one figure of a color different from the main color: 04 Remove the largest figure of a color different from the main color. 05 If there is a figure of the main color: 06 Remove the largest figure of the main color.
If there are several ways to remove the figure in lines 04 and 06, one should choose the largest figure containing the bottommost ball (if there are several such figures, then one should choose among them the figure that contains the leftmost of such balls).
Chairman is the laziest person in the jury. He doesn't even think about which color he should choose as the main one. By pressing one key, he launches a program that calculates for every color present in the field the number of points that will be awarded if this color is chosen as the main one. Your task is to write such a program.
Input
The first line contains the dimensions of the field n and m (1 ≤ n, m ≤ 50). Each of the following n lines contains m letters denoting the color of the ball in the corresponding cell of the field (B for blue, G for green, R for red, W for white, and Y for yellow). The rows of the playing field are given in the order from the top row to the bottom row.
Output
Output one line for each color present in the field: first output the letter denoting the color, then a colon, a space, and the number of points the chairman of the jury will get if he chooses this color as the main one. The colors must be considered in the following order: blue, green, red, white, yellow.
Sample Input
input | output |
---|---|
3 6 WWWGBG WBGGGB GGGGBB |
B: 74 G: 92 W: 74 |
1 #include<stdio.h> 2 #include<string.h> 3 4 int n,m; 5 char maps[56][56]; 6 int r[5]={0,-1,0,1,0},c[5]={0,0,-1,0,1}; 7 int mapfigure[56][56],s,vmap[56][56]; 8 int copymap[56][56],bottommost[56][56],leftmost[56][56],bottomm,leftm; 9 10 void serch(int a,int b) 11 { 12 int i,j; 13 if(vmap[a][b]!=0 || copymap[a][b]==0) 14 return; 15 s++;mapfigure[a][b]=s;vmap[a][b]=1; 16 if(a>bottomm) 17 bottomm=a; 18 if(b<leftm) 19 leftm=b; 20 bottommost[a][b]=bottomm,leftmost[a][b]=leftm; 21 for(i=1;i<=4;i++) 22 { 23 int x=a+r[i],y=b+c[i]; 24 if(1<=x && x<=n && 1<=y && y<=m && vmap[x][y]==0 && copymap[x][y]==copymap[a][b]) 25 { 26 serch(x,y); 27 } 28 } 29 return; 30 } 31 32 void clearr(int a,int b) 33 { 34 int i,j; 35 if(copymap[a][b]==0) 36 return; 37 copymap[a][b]=0; 38 for(i=1;i<=4;i++) 39 { 40 int x=a+r[i],y=b+c[i]; 41 if(1<=x && x<=n && 1<=y && y<=m && copymap[x][y]==copymap[a][b]) 42 { 43 clearr(x,y); 44 } 45 } 46 return; 47 } 48 49 void move() 50 { 51 int i,j,k; 52 for(j=1;j<=m;j++) 53 { 54 for(i=n;i>=1;i--) 55 { 56 if(copymap[i][j]==0) 57 { 58 for(k=i-1;k>=1;k--) 59 { 60 if(copymap[k][j]!=0) 61 { 62 copymap[i][j]=copymap[k][j]; 63 copymap[k][j]=0; 64 break; 65 } 66 } 67 } 68 } 69 } 70 for(j=1;j<=m;j++) 71 { 72 if(copymap[n][j]==0) 73 { 74 for(k=j;k<m;k++) 75 { 76 for(i=1;i<=n;i++) 77 copymap[i][k]=copymap[i][k+1]; 78 } 79 m--;j--; 80 } 81 } 82 return; 83 } 84 85 int main() 86 { 87 int i,j,k; 88 while(scanf("%d %d",&n,&m)!=EOF) 89 { 90 getchar(); 91 for(i=1;i<=n;i++) 92 { 93 for(j=1;j<=m;j++) 94 { 95 scanf("%c",&maps[i][j]); 96 } 97 getchar(); 98 } 99 100 /*for(i=1;i<=n;i++) 101 { 102 for(j=1;j<=m;j++) 103 { 104 printf("%c",maps[i][j]); 105 } 106 printf(" "); 107 }*/ 108 int maincolor; 109 for(maincolor=1;maincolor<=5;maincolor++) 110 { 111 int ans=0; 112 int flg=0; 113 for(i=1;i<=n;i++) 114 { 115 for(j=1;j<=m;j++) 116 { 117 if(maps[i][j]=='B') 118 copymap[i][j]=1; 119 else if(maps[i][j]=='G') 120 copymap[i][j]=2; 121 else if(maps[i][j]=='R') 122 copymap[i][j]=3; 123 else if(maps[i][j]=='W') 124 copymap[i][j]=4; 125 else if(maps[i][j]=='Y') 126 copymap[i][j]=5; 127 if(copymap[i][j]==maincolor) 128 flg=1; 129 } 130 } 131 if(flg==0) 132 { 133 continue; 134 } 135 136 while(1) 137 { 138 memset(vmap,0,sizeof(vmap)); 139 memset(bottommost,0,sizeof(bottommost)); 140 memset(leftmost,0,sizeof(leftmost)); 141 for(i=n;i>=1;i--) 142 { 143 for(j=1;j<=m;j++) 144 { 145 s=0,bottomm=1,leftm=m; 146 serch(i,j); 147 } 148 } 149 int ok=0; 150 for(i=1;i<=n;j++) 151 { 152 if(ok==1) break; 153 for(j=1;j<=m;j++) 154 { 155 if(mapfigure[i][j]>1) 156 { 157 ok=1; 158 break; 159 } 160 } 161 } 162 if(ok==0) 163 break; 164 165 while(1) 166 { 167 168 memset(vmap,0,sizeof(vmap)); 169 memset(bottommost,0,sizeof(bottommost)); 170 memset(leftmost,0,sizeof(leftmost)); 171 for(i=n;i>=1;i--) 172 { 173 for(j=1;j<=m;j++) 174 { 175 s=0,bottomm=1,leftm=m; 176 serch(i,j); 177 } 178 } 179 int largestfigure=0,lagi,lagj; 180 for(i=1;i<=n;i++) 181 { 182 for(j=1;j<=m;j++) 183 { 184 if(copymap[i][j]!=maincolor) 185 { 186 if(mapfigure[i][j]>largestfigure) 187 { 188 largestfigure=mapfigure[i][j]; 189 lagi=i,lagj=j; 190 } 191 else if(mapfigure[i][j]==largestfigure && bottommost[i][j]>bottommost[lagi][lagj]) 192 { 193 largestfigure=mapfigure[i][j]; 194 lagi=i,lagj=j; 195 } 196 else if(mapfigure[i][j]==largestfigure && bottommost[i][j]==bottommost[lagi][lagj] && leftmost[i][j]<leftmost[lagi][lagj]) 197 { 198 largestfigure=mapfigure[i][j]; 199 lagi=i,lagj=j; 200 } 201 } 202 } 203 } 204 if(largestfigure<2) 205 break; 206 ans=ans+largestfigure*(largestfigure-1); 207 clearr(lagi,lagj); 208 move(); 209 } 210 211 memset(vmap,0,sizeof(vmap)); 212 memset(bottommost,0,sizeof(bottommost)); 213 memset(leftmost,0,sizeof(leftmost)); 214 for(i=n;i>=1;i--) 215 { 216 for(j=1;j<=m;j++) 217 { 218 s=0,bottomm=1,leftm=m; 219 serch(i,j); 220 } 221 } 222 int mainfigure=0,maini,mainj; 223 for(i=1;i<=n;i++) 224 { 225 for(j=1;j<=m;j++) 226 { 227 if(copymap[i][j]==maincolor && mapfigure[i][j]>mainfigure) 228 { 229 mainfigure=mapfigure[i][j]; 230 maini=i,mainj=j; 231 } 232 } 233 } 234 if(mainfigure>1) 235 { 236 ans=ans+mainfigure*(mainfigure-1); 237 clearr(maini,mainj); 238 move(); 239 } 240 241 } 242 243 244 if(maincolor==1) 245 printf("B: "); 246 else if(maincolor==2) 247 printf("G: "); 248 else if(maincolor==3) 249 printf("R: "); 250 else if(maincolor==4) 251 printf("W: "); 252 else if(maincolor==5) 253 printf("Y: "); 254 printf("%d ",ans); 255 } 256 } 257 return 0; 258 }