poj1027 The Same Game

The Same Game
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5459 Accepted: 2066

Description

The game named "Same" is a single person game played on a 10 \Theta 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 below causes: 
 
The objective of the game is to remove every ball from the board, and the game is over when every ball is removed or when every cluster has only one ball. The scoring of each game is as follows. The player starts with a score of 0. When a cluster of m balls is removed, the player's score increases by (m-2)^2 . A bonus of 1000 is given if every ball is removed at the end of the game. 
You suspect that a good strategy might 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 should choose the leftmost ball giving the largest cluster. If there is still a tie, it should 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 and 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 point bonus if all the balls are removed after the move. 
The final score should be reported as follows: 
Final score: s, with b balls remaining. 
Insert a blank line between the output of each game. Use the plural forms "balls" and "points" even if the corresponding value 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. 

Source


题意:借用小优的话

在一个固定大小为10x15的矩形区域A内被RGB三种颜色的小球填满

现在按如下步骤操作:

1、  删除区域A内最大的一片区域M(任意颜色都可以,只要其占有区域最大)

2、  删除M后,自然会出现空的位置,在M区域上方的小球自然下落;

当删除M后出现空列时,右边的列往左填充。

注意是以“列”为单位填充,非空列只能整列往空列移动。

移动后,各个小球之间的相对顺序 与 移动前一样。

3、  当区域A剩余小球数为0,或A内的最大区域为1时,游戏结束。否则返回1。

 

输出每一步的得分,最后输出总得分。

思路:

模拟:

1.找到最大连通块。

2.删除这些连通块。

3.从上往下棋子依次落下。

4.如果出现了空列那么向左移动过来。

QAQ一开始求连通块按照小优的思路写了个bfs,一直TLE,我还想是不是哪里边界想错了,但是怎么找也找不出来,后来想了想,可能用stl的队列是比较费时的。

所以我直接写了个dfs,过了。qwq代码更少呦。

注意:

1.要求输出的是连通块中左下角的球的坐标。

2.用bfs,stl的队列会超时QAQ。

3.模拟一定要细心,错一点就是错。

4.最后如果球全部取完要加上1000分。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN=20;
struct node
{
    int x,y;
};
int maxi,maxj;
int p;
char maxcolour,colour;
bool vis[MAXN][MAXN];
char tu[MAXN][MAXN];
int xx[4]= {1,0,-1,0};
int xy[4]= {0,1,0,-1};
//搜索当前相同颜色块的大小
void dfs(int x,int y)
{
    int i;
    vis[x][y]=1;
    p++;
    for( i=0; i<4; ++i)
    {
        int nx=x+xx[i];
        int ny=y+xy[i];
        if(nx>=0&&nx<10&&ny>=0&&ny<15&&!vis[nx][ny]&&tu[nx][ny]==colour)
        {
            vis[nx][ny]=1;
            dfs(nx,ny);
        }
    }
}
//寻找最大的相同颜色块
int search_max_area()
{
    int i,j;
    memset(vis,0,sizeof(vis));
    int MAX=0;
    for( j=0; j<15; ++j)
        for( i=0; i<10; ++i)
        {
            if(!vis[i][j]&&tu[i][j])
            {
                colour=tu[i][j];
                p=0;
                dfs(i,j);
                if(MAX<p)
                {
                    MAX=p;
                    maxi=i;
                    maxj=j;
                }
            }
        }
    maxcolour=tu[maxi][maxj];
    return MAX;
}
//删除找到的最大相同颜色的块
void del_tu(int x,int y)
{
    int i;
    tu[x][y]=0;
    for( i=0; i<4; ++i)
    {
        int nx=x+xx[i];
        int ny=y+xy[i];
        if(nx>=0&&nx<10&&ny>=0&&ny<15&&tu[nx][ny]==maxcolour)
        {
            del_tu(nx,ny);
        }
    }
}
void updata_tu()
{
    int i,j;
    bool lie[15]= {0};
    //从第一列开始,从上向下落子
    for( j=0; j<15; ++j)
    {
        bool flag=1;//标记这一列为空
        int pre=-1;
        for( i=0; i<10; ++i)
        {
            if(tu[i][j])
            {
                flag=0;//不为空
                if(pre!=-1)
                {
                    tu[pre][j]=tu[i][j];
                    tu[i][j]=0;
                    i=pre;
                    pre=-1;
                }
            }
            else
            {
                pre=i;
                while(i+1<10&&!tu[i+1][j])i++;
            }
        }
        if(flag)lie[j]=1;
    }
    //从右向左靠齐
    int pre=-1;//-1表示这一列前面没有空列
    for( j=0; j<15; ++j)
    {
        if(!lie[j])
        {
            if(pre!=-1)//如果存在空列
            {
                for( i=0; i<10; ++i)
                {
                    tu[i][pre]=tu[i][j];//移动
                    tu[i][j]=0;//清空
                }
                lie[j]=1;//此列变空
                j=pre;//操作完应当从空列的下一列开始
                pre=-1;//没空列

            }
        }
        else
        {
            pre=j;
            while(j+1<15&&lie[j+1])j++;
        }
    }
}

int main()
{
    int t;
    int i,k;
    scanf("%d",&t);
    for( k=1; k<=t; ++k)
    {
        for( i=9; i>=0; --i)scanf("%s",tu[i]);
        printf("Game %d:\n\n",k);
        int step=0,ball=150,sumscore=0;
        while(1)
        {
            int max_area=search_max_area();
            if(max_area==1||max_area==0)break;
            step++;
            int score=(max_area-2)*(max_area-2);
            printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n",step,maxi+1,maxj+1,max_area,maxcolour,score);
            ball-=max_area;
            sumscore+=score;
            del_tu(maxi,maxj);
            updata_tu();
        }
        if(!ball)sumscore+=1000;
        printf("Final score: %d, with %d balls remaining.\n\n",sumscore,ball);
    }
    return 0;
}

模拟题就是恶心(⊙v⊙)嗯




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值