POJ 1027--The Same Game

题意

这道题比较简单,就是一个消除游戏,求最终能得到的最高分数,并输出每一次消除的分数,颜色,个数,和消除区域最左、最下边的坐标。

分析

因为在出现空列的时候,需要将右边部分合并到左边,所以考虑按列处理,而不是按行。

我们把每一列看作一个字符串,那么所有球构成一个初始长度为10的15个字符串。这里利用了一个string的特性,string在中间某一字符被删除的时候会自动收缩。比如,str = “abcd”,在删除字符‘b’之后,str自动变为”acd”,这样避免了手动去移动字符。

在搜索最大区域的时候,假定最后需要N次来消除,那么我们在搜索的时候把每个字符均加上当前次数n[1, N]在每次消除的时候,所有字符均变为 c(原值) + n(1..N)。因为每次搜索的起点[x,y]有记录,最后删除的时候直接从坐标[x,y]开始去删除邻近的值等于matrix[x,y]的小球就行。

搜索和删除最大区域均用了一个函数int search(int pos, int col, char key, bool erase),pos为当前搜索列的下标(从下到上),也就是字符串的下标。col为当前所有列的数目,也就是字符串数目。key为搜索或删除目标,通过erase确定是删除还是搜索。

因为是深度搜索,所以删除的时候,先标记要删除的字符,最后遍历一次删除即可。

PS:注意输出格式。

代码如下:

Memory: 220K Time: 500MS Length: 93LINES

#include <iostream>
#include <string>
using namespace std;

const unsigned int max_rows = 10;
const unsigned int max_cols = 15;
unsigned int cols;

string matrix[max_cols];

int search(int pos, int col, char key, bool erase)
{
    int left, right;
    for (left = pos; left >= 0; --left)
    {
        if (matrix[col][left] == key) matrix[col][left] = erase ? '0' : matrix[col][left] + 1;
        else       break;
    }
    ++left;
    for (right = pos + 1; right < matrix[col].length(); ++right)
    {
        if (matrix[col][right] == key) matrix[col][right] = erase ? '0' : matrix[col][right] + 1;
        else       break;
    }
    int sum = right - left;
    for (int i = left; i < right; ++i)
    {
        if (col < cols - 1 && matrix[col + 1].length() > i && matrix[col + 1][i] == key)
            sum += search(i, col + 1, key, erase);
        if (col > 0 && matrix[col - 1].length() > i && matrix[col - 1][i] == key)
            sum += search(i, col - 1, key, erase);
    }
    return sum;
}

bool check(char c) { return (c == 'R' || c == 'G' || c == 'B'); }

int main()
{
    int count = 0;
    cin >> count;
    for (int i = 0; i < count; ++i)
    {
        char c;
        short remaind = max_rows * max_cols;
        int final_score = 0;
        for (int j = 0; j < max_cols; ++j) matrix[j].clear();
        for (int j = 0; j < max_rows; ++j)
            for (int k = 0; k < max_cols && cin >> c && check(c); matrix[k] = c + matrix[k], ++k);

        cout << "Game " << i + 1 << ":" << endl << endl;
        cols = max_cols;
        for (int step = 1, maxsum = 2; maxsum > 1; ++step, remaind -= maxsum)
        {
            maxsum = 0;
            int j, k;
            int x = -1, y = -1;
            int zone = 0;
            for (j = 0; j < cols; ++j)
                for (k = 0; k < matrix[j].length(); ++k)
                {
                    if (check(matrix[j][k] - step + 1))
                    {
                        int res = search(k, j, matrix[j][k], false);
                        if (res > maxsum)
                        {
                            maxsum = res;
                            x = j;
                            y = k;
                        }
                    }
                }
            if (maxsum < 2) break;
            int score = (maxsum - 2)*(maxsum - 2);
            final_score += score;
            cout << "Move " << step << " at (" << y + 1 << "," << x + 1 << "): removed " << maxsum;
            cout << " balls of color " << char(matrix[x][y] - step) << ", got " << score << " points." << endl;
            search(y, x, matrix[x][y], true);
            for (j = 0; j < cols; ++j)
            {
                for (string::size_type pos; (pos = matrix[j].find('0')) != string::npos; matrix[j].erase(pos, 1));
                if (matrix[j].length() == 0)
                {
                    for (int k = j; k < cols - 1; ++k)  matrix[k] = matrix[k + 1];
                    --j;
                    --cols;
                }
            }
        }
        cout << "Final score: " << final_score + (remaind == 0 ? 1000 : 0) << ", with " << remaind << " balls remaining." << endl << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值