1027:The Same Game

描述
名为“Same”的游戏是在10 \ Theta 15板上玩的单人游戏。每个方块包含一个红色(R),绿色(G)或蓝色(B)的球。如果两个球具有相同的颜色,则属于同一簇,并且可以通过在上,下,左,右四个方向上跟随相同颜色的球来从另一个球到达。在游戏的每个步骤中,玩家选择一个球,其球簇至少有两个球,并从棋盘中移除群集中的所有球。然后,电路板分两步“压缩”:
1.将每列中剩余的球向下移动以填充空白区域。保留每列中球的顺序。
2.如果列变空,请尽可能将剩余列向左移动。列的顺序将保留。
例如,选择下面子板左下角的球会导致:
游戏的目标是从棋盘上移除每一个球,当每个球被移除或每个球只有一个球时游戏结束。每场比赛的得分如下。玩家以0分开始。当移除一组m球时,玩家的分数增加(m-2)^ 2。如果在比赛结束时移除每个球,则奖励1000。
你怀疑一个好的策略可能是选择在每一步给出最大可能集群的球,并且你想通过编写一个程序来模拟使用这种策略进行的游戏来测试这个策略。如果有两个或更多球可供选择,程序应选择最左边的球给出最大的球。如果仍有领带,则应选择这些最左侧球的最底部球。
输入
您将在输入中获得许多游戏。第一行输入包含一个正整数,给出了要遵循的游戏数量。每个游戏的球的初始排列从上到下一次一行。每行包含15个字符,每个字符是“R”,“G”或“B”之一,指定从左到右的行中球的颜色。每场比赛前都有一个空行。
输出
对于每个游戏,打印游戏编号,然后是新行,然后是每个移动的信息,然后是最终得分。每个动作都应以以下格式打印:
在(r,c)处移动x:移除了颜色为C的球,得到了s点。
其中x是移动数,r和c分别是所选球的行数和列数。行从底部开始编号为1到10,列从左侧开始编号为1到15。 b是移除的群集中的球数。 C是“R”,“G”或“B”中的一个,表示去除的球的颜色。 s是此举的得分。如果在移动后移除所有球,则得分不包括1000点奖励。
最终得分应报告如下:
最终得分:s,剩下b球。
在每个游戏的输出之间插入一个空行。即使相应的值为1,也使用复数形式“球”和“点”。
样例输入
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
样例输出
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;
}

来源:https://www.cnblogs.com/dengeven/p/3228977.html

猜你喜欢

转载自www.cnblogs.com/sweet-ginger-candy/p/11518210.html
今日推荐