poj 1027 The Same Game

The Same Game
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5435 Accepted: 2055

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

提示

题意:
10*15(下边行最小,左边列最小)的矩阵中找出相同颜色最大面积的区域并消去,得分为(消去小球的个数-2)^2,相同颜色面积计算就是该区域上下左右有相同颜色的小球则面积加1,每操作一次输出一次详细情况:"Move 当前第几步 at (该区域最小行列小球的坐标,列优先): removed 该区域小球数 balls of color 颜色种类, got 得分 points. ",且小球会因为重力掉落到底部或者小球的上方。直到小球总数为0或者最大面积为1。最后输出总分和剩余小球数。
如果剩余小球数为0,奖励1000分。有多个相同的最大面积区域则取列最小的那一区域。如果在消除的过程中某一列没有小球,右边的有小球的一列就会自动的移过来(整列)。
思路:
bfs暴力枚举最大面积区域,记录每个小球的坐标便于消除,要注意题目的行列最小的位置。

示例程序

Source Code

Problem: 1027		Code Length: 2970B
Memory: 360K		Time: 422MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
struct
{
    int x,y;
}p[150],p1[150];			//p1暂时用来存枚举每个面积的小球坐标,p为最终结果
char map[10][16];
int wide,minx,miny,v[10][15],walk[4][2]={{1,0},{-1,0},{0,1},{0,-1}};		//minx,miny是每个面积最小行和列
void updata()
{
    int i,i1,i2,pos=0;
    for(i=0;wide>i;i++)
    {
        i2=9;
        for(i1=9;i1>=0;i1--)
        {
            if(map[i1][i]!='0')
            {
                map[i2][i-pos]=map[i1][i];
                i2--;
            }
        }
        if(i2==9)		//当前列无小球,移动到前pos列
        {
            pos++;
        }
        else
        {
            while(i2>=0)			//填充
            {
                map[i2][i-pos]='0';
                i2--;
            }
        }
    }
    wide=wide-pos;			//宽度减少
}
int bfs(char c,int x,int y)
{
    int i,f=0,top=0;
    p1[0].x=x;
    p1[0].y=y;
    minx=10-x;
    miny=y+1;
    v[x][y]=1;
    top++;
    while(f<top)
    {
        for(i=0;4>i;i++)
        {
            x=p1[f].x+walk[i][0];
            y=p1[f].y+walk[i][1];
            if(x<10&&x>=0&&y<wide&&y>=0&&v[x][y]==0&&map[x][y]==c)
            {
                p1[top].x=x;
                p1[top].y=y;
                v[x][y]=1;
                if(miny>y+1||(miny==y+1&&minx>10-x))				//记录该面积最小行列
                {
                    minx=10-x;
                    miny=y+1;
                }
                top++;
            }
        }
        f++;
    }
    return top;
}
int main()
{
    int i,i1,i2,i3,i4,t,sum,num,max,temp,x,y;
    char c;
    scanf("%d",&t);
    for(i=1;t>=i;i++)
    {
        sum=0;					//总分
        num=150;				//小球剩余个数
        wide=15;				//初始化矩阵宽度
        max=0;					//最大面积
        for(i1=0;10>i1;i1++)
        {
            scanf("%s",map[i1]);
        }
        printf("Game %d:\n\n",i);
        for(i1=1;max!=1&&num!=0;i1++)
        {
            max=0;
            memset(v,0,sizeof(v));
            for(i2=9;i2>=0;i2--)				//枚举每个小球,求最大面积,从行最小开始
            {
                for(i3=0;wide>i3;i3++)
                {
                    if(map[i2][i3]=='0'||v[i2][i3]==1)			//当前格子无小球或者已经过搜索
                    {
                        continue;
                    }
                    temp=bfs(map[i2][i3],i2,i3);
                    if(max<temp||(max==temp&&y>miny))			//最大面积且列最小,进行更新
                    {
                        x=minx;
                        y=miny;
                        max=temp;
                        c=map[i2][i3];
                        for(i4=0;temp>i4;i4++)
                        {
                            p[i4]=p1[i4];
                        }
                    }
                }
            }
            if(max!=1)
            {
                num=num-max;				//数据以及地图的更新
                sum=sum+(max-2)*(max-2);
                for(i2=0;max>i2;i2++)
                {
                    map[p[i2].x][p[i2].y]='0';
                }
                updata();
                printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points. \n",i1,x,y,max,c,(max-2)*(max-2));
            }
        }
        if(num==0)				//奖励分
        {
            sum=sum+1000;
        }
        printf("Final score: %d, with %d balls remaining. \n\n",sum,num);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值