Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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 6WWWGBGWBGGGBGGGGBB | B: 74G: 92W: 74 |
1 #include2 #include 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 =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 =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] =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\n",ans);255 }256 }257 return 0;258 }