Problem descriptioncontinuous same game is a simple game played on a grid of colored blocks. groups of two or more connected (orthogonally, not diagonally) blocks that are the same color may be removed from the board. when a group of blocks is removed, the blocks above those removed ones fall down into the empty space. when an entire column of blocks is removed, all the columns to the right of that column shift to the left to fill the empty columns. points are scored whenever a group of blocks is removed. the number of points per block increases as the group becomes bigger. when N blocks are removed, N * (N-1) points are scored.
Ll was interested in this game at one time, but he found it is so difficult to find the optimal scheme. so he always play the game with a greedy strategy: choose the largest group to remove and if there are more than one largest group with equal number of blocks, choose the one which contains the most preceding block (x1, Y1) is in front of (X2, Y2) if and only if (X1 <X2 | X1 = X2 & Y1 <Y2 )). now, He want to know How does points he will get. Can you help him?
Inputeach test case begins with two integers n, m (5 <= n, m <= 20), which is the size of the Board. then n lines follow, each contains M characters, indicating the color of the block. there are 5 colors, and each with equal probability.
Outputfor each test case, output a single line containing the total point he will get with the greedy strategy.
Sample Input
5 53555231154332222113412314
Sample output
32Hint35552 00552 00002 00002 00000 0000031154 05154 05104 00004 00002 0000033222 01222 01222 00122 00104 0010021134 21134 21134 25234 25234 2523012314 12314 12314 12314 12314 12312The total point is 12+6+6+2+6=32.
Train of Thought: DFS finds the largest connected block to eliminate it. Note that when moving left, two consecutive columns are empty.
# Include <stdio. h> int n, m, total, NXT [4] [2] = {0,-1 }}; bool vis [20] [20]; char d [20] [21]; void DFS (int x, int y, int num) {for (INT I = 0; I <4; I ++) {x + = NXT [I] [0]; y + = NXT [I] [1]; if (x> = 0 & x <n & Y> = 0 & Y <M &&! Vis [x] [Y] & D [x] [Y] = num) {vis [x] [Y] = 1; Total ++; DFS (X, y, num) ;}x-= NXT [I] [0]; y-= NXT [I] [1] ;}} void Tran (INT X, int y, int num) {for (INT I = 0; I <4; I ++) {x + = NXT [I] [0]; Y + = NXT [I] [1]; if (x> = 0 & x <n & Y> = 0 & Y <M & D [x] [Y] = num) {d [x] [Y] = '0'; Tran (X, Y, num);} X-= NXT [I] [0]; y-= NXT [I] [1] ;}} int main () {int I, J, K, MX, X, Y, ANS, remain, T; while (~ Scanf ("% d", & N, & M) {for (I = 0; I <n; I ++) scanf ("% s ", d [I]); ans = 0; remain = N * m; while (remain) {MX = 0; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) vis [I] [J] = 0; for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (d [I] [J]> '0 '&&! Vis [I] [J]) {vis [I] [J] = 1; Total = 1; DFS (I, j, d [I] [J]); if (total> MX) {MX = total; X = I; y = J ;}}} remain-= Mx; ans + = Mx * (mx-1 ); tran (X, Y, d [x] [Y]); D [x] [Y] = '0'; for (I = n-1; I> = 0; I --) // move down {for (j = 0; j <m; j ++) {If (d [I] [J] = '0 ') {for (k = I-1; k> = 0; k --) {If (d [k] [J]> '0 ') {d [I] [J] = d [k] [J]; d [k] [J] = '0'; break ;}}} T = s-1; while (t --) // move to the left. Note that the two consecutive columns are empty. {for (j = 0; j <m-1; j ++) {for (I = 0; I <n; I ++) if (d [I] [J]> '0') break; if (I = N) {for (I = 0; I <n; I ++) {d [I] [J] = d [I] [J + 1]; d [I] [J + 1] = '0' ;}}} printf ("% d \ n", ANS );}}