The Same Game 1027 poj

题目来源:http://poj.org/problem?id=1027

题目大意:

  题目说的就是现在蛮流行的手机小游戏popstar,求用贪心方法能得到多少分。

  小球有三种颜色:R/G/B。横向、纵向相连的同色小球可以同时被消掉。消掉一些小球后,首先空格上面的小球会下落,填补空白,然后如果中间出现空的列,则空列右侧的小球左移,填补空列。

  当m个球被消掉时,奖分(m-2)^2,若所有球都被消掉,奖分1000.贪心策略是每次选择消去连在一起数目最多的球。当有多个时,找最左最下的球所在的那一群。

输入:第一行为测试用例数。每个用例由一系列字符串组成,表示初始时小球的分布。

输出:每步选择的小球,每步消掉的小球数,得到的分数,以及最后的得分和剩余小球数。

具体形式见Sample


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;
}

 

posted @ 2018-10-08 19:09  soqu36  阅读(171)  评论(0)    收藏  举报