CSUOJ 1862 The Same Game 模拟消消乐

Description

    The game named "Same" is a single-person game played on a 10 by 15 board. Each square contains a ball colored red (R), green (G), or blue (B). Two balls belong to the same cluster if they have the same color, and one can be reached from another by following balls of the same color in the four directions up, down, left, and right. At each step of the game, the player chooses a ball whose cluster has at least two balls and removes all balls in the cluster from the board. Then, the board is "compressed" in two steps:

  1. Shift the remaining balls in each column down to fill the empty spaces. The order of the balls in each column is preserved. 
  2. If a column becomes empty, shift the remaining columns to the left as far as possible. The order of the columns is preserved. 

    For example, choosing the ball at the bottom left corner in the sub-board causes: 

    The objective of the game is to remove all balls from the board, and the game is over when all balls are removed or when all clusters have only one ball.
    The scoring of each game is as follows. The player starts with a score of 0. When a cluster of size m is removed, the player's score increases by (m-2)^2. A bonus of 1000 is given if all balls are removed at the end of the game.
    You suspect that a good strategy may be to choose the ball that gives the largest possible cluster at each step, and you want to test this strategy by writing a program to simulate games played using this strategy. If there are two or more balls to choose from, the program chooses the leftmost ball giving the largest cluster. If there is still a tie, choose the bottommost ball of these leftmost balls.

Input

    You will be given a number of games in the input. The first line of input contains a positive integer giving the number of games to follow. The initial arrangement of the balls of each game is given one row at a time, from top to bottom. Each row contains 15 characters, each of which is one of "R", "G", or "B", specifying the colors of the balls in the row from left to right. A blank line precedes each game.

Output

    For each game, print the game number, followed by a new line, followed by information about each move, followed by the final score. Each move should be printed in the format:

Move x at (r,c): removed b balls of color C, got s points.

    where:

  • x is the move number.
  • (r,c) are the row number and column number of the chosen ball, respectively. The rows are numbered from 1 to 10 from the bottom, and columns are numbered from 1 to 15 from the left.
  • b is the number of balls in the cluster removed.
  • C is one of "R", "G", or "B", indicating the color of the balls removed.
  • s is the score for this move. The score does not include the 1000 bonus if the move removes all balls.

    The final score should be reported as follows:

Final score: s, with b balls remaining.

    Note: use the plural form of "ball" and "point" even if it is 1.

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.
  题目说白了就是模拟消消乐。。。每一次选择图上最大的连通块消去,问最后的分数

  其实相同了还是很好写的。。突然想出了一种数据结构叫十字链表,感觉可以用得上。。但是懒得写了

  关于从图上移除空白的办法借鉴了STL中unique的写法,挺简单和优美的,总之就是一道模拟题。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;
typedef pair<int,int> pa;
const int dx[]{0,0,1,-1},dy[]{1,-1,0,0};

char board[20][20];
bool vis[20][20];
struct Node{
    int x,y,cost;
    char color;
    explicit Node(int a=0,int b=0,int c=0,char d=0):x(a),y(b),cost(c),color(d){};
};
Node getRemove();
int bfs(pa p),remain,k,kcase,ans;
void _remove(pa p);
void print(){
    for(int i=10;i;--i){
        for(int j=1;j<=15;++j)
            cout<<board[i][j];
        cout<<endl;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    //freopen("te.txt","w",stdout);
    int times;
    cin>>times;
    while(times--){
        for(int i=10;i;--i)
            cin>>(1+board[i]);
        cout<<"Game "<<++kcase<<":\n\n";
        k=ans=0;remain=150;
        memset(vis,0,sizeof vis);
        Node te;
        while(remain){
            te=getRemove();
            if(te.x==0)
                break;
            cout<<"Move "<<++k<<" at ("<<te.x<<","<<te.y<<"): removed "<<te.cost<<" balls of color "<<te.color<<", got "<<((te.cost-2)*(te.cost-2))<<" points.\n";
            ans+=(te.cost-2)*(te.cost-2);
        }
        cout<<"Final score: "<<(ans+(remain?0:1000))<<", with "<<remain<<" balls remaining.\n\n";
    }
    return 0;
}

Node getRemove(){
    int res=1,x=0,y=0;
    for(int j=1,k;board[1][j];++j)
    for(int i=1;board[i][j];++i)
    if(!vis[i][j]&&res<(k=bfs(make_pair(i,j))))
        res=k,x=i,y=j;


    Node tmp(x,y,res,board[x][y]);
    if(tmp.x)
        _remove(make_pair(x,y)),remain-=res;
    //print();
    return tmp;
}

int bfs(pa p){
    int res=0;
    queue<pa> q;
    q.push(p);
    vis[p.first][p.second]=true;
    while(!q.empty()){
        p=q.front();q.pop();++res;
        for(int i=0;i<4;++i)
        if(!vis[p.first+dx[i]][p.second+dy[i]]&&board[p.first+dx[i]][p.second+dy[i]]==board[p.first][p.second])
            vis[p.first+dx[i]][p.second+dy[i]]=true,q.push(make_pair(p.first+dx[i],p.second+dy[i]));
    }
    return res;
}

void _remove(pa p){
    char c=board[p.first][p.second];
    queue<pa> q;
    q.push(p);
    while(!q.empty()){
        p=q.front();q.pop();
        board[p.first][p.second]=0;
        for(int i=0;i<4;++i)
        if(board[p.first+dx[i]][p.second+dy[i]]==c)
            q.push(make_pair(p.first+dx[i],p.second+dy[i]));
    }

//    cout<<"AAAAA"<<endl;
//    print();
//    cout<<endl;

    int k1,k2;
    for(int j=1;j<=15;++j){
        for(k1=1,k2=1;k2<=10;){
            if(board[k2][j])
                board[k1++][j]=board[k2++][j];
            else
                ++k2;
        }
        for(;k1<=10;++k1)
            board[k1][j]=0;
    }

    for(k1=k2=1;k2<=15;){
        if(board[1][k2]){
            for(int i=1;i<=10;++i)
                board[i][k1]=board[i][k2];
            ++k1,++k2;
        }else
            ++k2;
    }
    for(;k1<=15;++k1)
    for(int i=1;i<=10;++i)
    board[i][k1]=0;

    memset(vis,0,sizeof vis);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值