1027:The Same Game

Description
game called "Same" is playing in the 10 \ Theta 15 single-player board. Each box contains a red (R), green (G) or blue (B) of the ball. If two balls have the same color, it belongs to the same cluster, and can be passed over, under, left, follow the same colored balls right directions to reach a ball from another. At each step of the game, the player chooses a ball, the spherical cluster of at least two goals, and remove all the balls in the cluster from the board. Then, the circuit board in two steps "compressed":
1. ball remaining in each column moved downward to fill the empty space. Each column in order to retain the ball.
2. If the column becomes empty, the remaining columns will move to the left as possible. Order of the columns will be retained.
For example, the lower left corner of the daughter board select the following ball cause:
Goal of the game is to remove every ball from the board, when each ball is removed or only one ball every ball game is over. Score every game is as follows. Players start with 0 points. Upon removal of a group of m ball, the player's score increased by (m-2) ^ 2. If you remove the ball at the end of each game, the 1000 award.
You suspect might be a good strategy given the greatest possible choice in every step of the cluster of balls, and you want to simulate the game using this strategy to test this strategy by writing a program. If there are two or more balls to choose from, the program should select the leftmost ball gives the maximum ball. If there is still a tie, you should choose the far left of the bottom ball ball.
Enter
you will get a lot of games in the input. The first line of input contains a positive integer that gives the number of games to follow. The initial arrangement of balls each game one line from top to bottom. Each row contains 15 characters, each character is one of "R", "G" or "B", the specified row from left to right in the color of the ball. There is a blank line before each game.
Output
For each game, print the game number, followed by a new line, followed by information for each move, followed by a final score. Each action should be printed in the following format:
in (r, c) of the mobile x: C removing the color of the ball to give the s-point.
Where x is the number of movements, r and c are the number of rows and columns of the selected balls. Bottom row numbered from 1 to 10, from the left column are numbered 1-15. b is the number of balls to remove a cluster. C is "R", "G" or "B" one, represents the color of the ball is removed. s is a move to score. If you remove all the balls after moving, the score does not include the 1,000-point bonus.
The final score should be reported as follows:
final score: s, b ball remaining.
Between the output of each game insert a blank line. Even if this value is 1, use the plural form "ball" and "point."
Sample input
3 
RGGBBGGRBRRGGBG 
RBGRBGRBGRBGRBG 
RRRRGBBBRGGRBBB 
GGRGBGGBRRGGGBG 
GBGGRRRRRBGGRRR 
BBBBBBBBBBBBBBB 
BBBBBBBBBBBBBBB 
RRRRRRRRRRRRRRR 
RRRRRRGGGGRRRRR 
GGGGGGGGGGGGGGG 

RRRRRRRRRRRRRRR 
RRRRRRRRRRRRRRR 
GGGGGGGGGGGGGGG 
GGGGGGGGGGGGGGG 
BBBBBBBBBBBBBBB 
BBBBBBBBBBBBBBB 
RRRRRRRRRRRRRRR 
RRRRRRRRRRRRRRR 
GGGGGGGGGGGGGGG 
GGGGGGGGGGGGGGG 

RBGRBGRBGRBGRBG 
BGRBGRBGRBGRBGR 
GRBGRBGRBGRBGRB 
RBGRBGRBGRBGRBG 
BGRBGRBGRBGRBGR 
GRBGRBGRBGRBGRB 
RBGRBGRBGRBGRBG 
BGRBGRBGRBGRBGR 
GRBGRBGRBGRBGRB 
RBGRBGRBGRBGRBG
Sample Output
Game 1: 

Move 1 at (4,1): removed 32 balls of color B, got 900 points. 
Move 2 at (2,1): removed 39 balls of color R, got 1369 points. 
Move 3 at (1,1): removed 37 balls of color G, got 1225 points. 
Move 4 at (3,4): removed 11 balls of color B, got 81 points. 
Move 5 at (1,1): removed 8 balls of color R, got 36 points. 
Move 6 at (2,1): removed 6 balls of color G, got 16 points. 
Move 7 at (1,6): removed 6 balls of color B, got 16 points. 
Move 8 at (1,2): removed 5 balls of color R, got 9 points. 
Move 9 at (1,2): removed 5 balls of color G, got 9 points. 
Final score: 3661, with 1 balls remaining. 

Game 2: 

Move 1 at (1,1): removed 30 balls of color G, got 784 points. 
Move 2 at (1,1): removed 30 balls of color R, got 784 points. 
Move 3 at (1,1): removed 30 balls of color B, got 784 points. 
Move 4 at (1,1): removed 30 balls of color G, got 784 points. 
Move 5 at (1,1): removed 30 balls of color R, got 784 points. 
Final score: 4920, with 0 balls remaining. 

Game 3: 

Final score: 0, with 150 balls remaining.
//////////////////////////////////////////////////////////////////////////
//        POJ1027 The Same Game
//        Memory: 288K        Time: 500MS
//        Language: C++        Result: Accepted
//////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <stdio.h>

using namespace std;

int gameCnt;
char board[11][16];
int record[11][16];
int rows[15];
int maxRow = 0;
int maxCol = 0;
char color = ' ';
int maxCluster = 0;
int score;

int bfs(int row, int col) {
    if (board[row][col] == 0) {
        return 0;
    }
    color = board[row][col];
    int cluster = 1;
    record[row][col] = 1;
    if (board[row + 1][col] != 0 && record[row + 1][col] == 0 && board[row + 1][col] == color) {
        cluster += bfs(row + 1, col);
    }
    if (board[row - 1][col] != 0 && record[row - 1][col] == 0 && board[row - 1][col] == color) {
        cluster += bfs(row - 1, col);
    }
    if (board[row][col + 1] != 0 && record[row][col + 1] == 0 && board[row][col + 1] == color) {
        cluster += bfs(row, col + 1);
    }
    if (board[row][col - 1] != 0 && record[row][col - 1] == 0 && board[row][col - 1] == color) {
        cluster += bfs(row, col - 1);
    }
    return cluster;
}

void clear(int row, int col) {
    board[row][col] = 0;
    if (board[row + 1][col] != 0 && board[row + 1][col] == color) {
        clear(row + 1, col);
    }
    if (board[row - 1][col] != 0 && board[row - 1][col] == color) {
        clear(row - 1, col);
    }
    if (board[row][col + 1] != 0 && board[row][col + 1] == color) {
        clear(row, col + 1);
    }
    if (board[row][col - 1] != 0 && board[row][col - 1] == color) {
        clear(row, col - 1);
    }
}

void process(int row, int col) {
    clear(row, col);
    int r = 0, c = 0;
    for (c = 0; c < 15; ++c) {
        int i, j;
        for (i = 0; i < 10 && board[i][c] != 0; ++i) {
            continue;
        }
        for (j = i; j < 10 && board[j][c] == 0; ++j) {
            continue;
        }
        while (i < 10) {
            if (j > 10) {
                board[i++][c] = 0;
                continue;
            }
            if (board[j][c] == 0) {
                ++j;
                continue;
            }
            board[i++][c] = board[j++][c];
        }
    }
    int i = 0, j = 0;
    for (i = 0; i < 15 && board[0][i] != 0; ++i) {
        continue;
    }
    for (j = i; j < 15 && board[0][j] == 0; ++j) {
        continue;
    }
    while (i < 15) {
        if (j > 15) {
            for (int k = 0; k <= 10; ++k) {
                board[k][i] = 0;
            }
            ++i;
            continue;
        }
        if (board[0][j] == 0) {
            ++j;
            continue;
        }
        for (int k = 0; k <= 10; ++k) {
            board[k][i] = board[k][j];
        }
        ++i;
        ++j;
    }
}

int main() {

    cin >> gameCnt;
    for (int gameId = 1; gameId <= gameCnt; ++gameId) {
        int row, col;
        memset(record, 0, sizeof(record));
        color = ' ';
        maxCluster = 0;
        for (row = 9; row >= 0; --row) {
            for (col = 0; col < 15; ++col) {
                cin >> board[row][col];
            }
        }
        int move = 1;
        int score = 0;
        int remain = 150;

        cout << "Game " << gameId << ":" << endl << endl;
        while (true) {
            maxCluster = 0;
            memset(record, 0, sizeof(record));
            for (row = 0, col = 0; board[row][col] != 0; ++col) {
                for (row = 0; board[row][col] != 0; ++ row) {
                    if (record[row][col] != 0) continue;
                    int cluster = bfs(row, col);
                    if (cluster > maxCluster) {
                        maxRow = row;
                        maxCol = col;
                        maxCluster = cluster;
                    }
                }
                row = 0;
            }
            color = board[maxRow][maxCol];
            if (maxCluster < 2) {
                break;
            }
            int point = (maxCluster - 2) * (maxCluster - 2);
            remain -= maxCluster;
            cout << "Move "<< move << " at (" << maxRow + 1 << ","<< maxCol + 1 
                << "): removed " << maxCluster <<" balls of color " << color << ", got " 
                << point << " points." << endl;
            ++move;
            score += point;
            process(maxRow, maxCol);
        }
        if (remain == 0) {
            score += 1000;
        }
        cout << "Final score: " << score << ", with " << remain << " balls remaining." << endl << endl;
    }

    system("pause");
    return 0;
}

Source: https: //www.cnblogs.com/dengeven/p/3228977.html

Guess you like

Origin www.cnblogs.com/sweet-ginger-candy/p/11518210.html