Description
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.
Input
Output
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("\n"); 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\n",ans); 255 } 256 } 257 return 0; 258 }