The Same Game-POJ1027模擬

The Same Game


Time Limit: 1000MS Memory Limit: 10000K

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.markdown

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.ui

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.this

Sample Input

3
RGGBBGGRBRRGGBG
RBGRBGRBGRBGRBG
RRRRGBBBRGGRBBB
GGRGBGGBRRGGGBG
GBGGRRRRRBGGRRR
BBBBBBBBBBBBBBB
BBBBBBBBBBBBBBB
RRRRRRRRRRRRRRR
RRRRRRGGGGRRRRR
GGGGGGGGGGGGGGGspa

RRRRRRRRRRRRRRR
RRRRRRRRRRRRRRR
GGGGGGGGGGGGGGG
GGGGGGGGGGGGGGG
BBBBBBBBBBBBBBB
BBBBBBBBBBBBBBB
RRRRRRRRRRRRRRR
RRRRRRRRRRRRRRR
GGGGGGGGGGGGGGG
GGGGGGGGGGGGGGGcode

RBGRBGRBGRBGRBG
BGRBGRBGRBGRBGR
GRBGRBGRBGRBGRB
RBGRBGRBGRBGRBG
BGRBGRBGRBGRBGR
GRBGRBGRBGRBGRB
RBGRBGRBGRBGRBG
BGRBGRBGRBGRBGR
GRBGRBGRBGRBGRB
RBGRBGRBGRBGRBGorm

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: ip

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. ci

Game 3:

Final score: 0, with 150 balls remaining.

Source

East Central North America 1999

題意:在一個固定大小爲10x15的矩形區域A內被RGB三種顏色的小球填滿。
如今按以下步驟操做:
1. 刪除區域A內最大的一片區域M(任意顏色均可以,只要其佔有區域最大)
2. 刪除M後,天然會出現空的位置,在M區域上方的小球天然下落;
當刪除M後出現空列時,右邊的列往左填充。
注意是以「列」爲單位填充,非空列只能整列往空列移動。
移動後,各個小球之間的相對順序 與 移動前同樣。
3. 當區域A剩餘小球數爲0,或A內的最大區域爲1時,遊戲結束。不然返回1。

輸出每一步的得分,最後輸出總得分。**

簡單的模擬,沒有什麼坑點

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
const int n = 10;
const int m = 15; 
char str[15][20];
int X,Y,num,sorce,t;
int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool vis[15][20];
bool Judge(int Fx,int Fy,char s)
{
    if(Fx>=0&&Fx<n&&Fy>=0&&Fy<m&&str[Fx][Fy]==s)
    {
        return true;
    }
    return false;
}
int dfs(int x,int y,char s)
{
    vis[x][y] = true;
    int ans= 1 ;
    for(int i=0;i<4;i++)
    {
        int Fx = x+dir[i][0];
        int Fy = y+dir[i][1];
        if(Judge(Fx,Fy,s)&&!vis[Fx][Fy])
        {
                ans += dfs(Fx,Fy,s);
        }
    }
    return ans;
}
bool BFS()
{
    memset(vis,false,sizeof(vis));
    int Max=  0,ans;
    for(int j=0;j<15;j++)
    {
        for(int i=0;i<10;i++)
        {
            if(!vis[i][j]&&str[i][j]!=0)
            {
                ans = dfs(i,j,str[i][j]);
                if(ans>Max)
                {
                    X = i;
                    Y = j;
                    Max = ans;
                }
            }
        }
    }
    if(Max>=2)
    {
        num+=Max;
        sorce+=(Max-2)*(Max-2);
        printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points. \n",t++,X+1,Y+1,Max,str[X][Y],(Max-2)*(Max-2));
    }
    return Max>=2;
}
void DFS(int x,int y,char s)
{
    str[x][y] = 0;
    for(int i=0;i<4;i++)
    {
        int Fx = x + dir[i][0];
        int Fy = y + dir[i][1];
        if(Judge(Fx,Fy,s))
        {
            DFS(Fx,Fy,s);
        }
    }
}
void Union()
{
    char s[15][20];
    bool visy[20];
    memset(visy,false,sizeof(visy));
    memset(s,0,sizeof(s));
    for(int i=0;i<m;i++)
    {
        int M = 0;
        for(int j=0;j<n;j++)
        {
            if(str[j][i]!=0)
            {
                s[M++][i]=str[j][i];
            }
        }
        if(M==0)
        {
            visy[i]=true;
        }
    }
    memset(str,0,sizeof(str));
    int M = 0;
    for(int i=0;i<m;i++)
    {
        if(!visy[i])
        {
            for(int j=0;j<n;j++)
            {
                str[j][M]=s[j][i];
            }
            M++;
        }
    }
}
int main()
{
    int T;
    int z = 1;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=9;i>=0;i--)
        {
            scanf("%s",str[i]);
        }
        if(z!=1)
        {
            printf("\n");
        }
        printf("Game %d:\n\n",z++);
        t=1,num = 0,sorce = 0;
        while(BFS())
        {
            DFS(X,Y,str[X][Y]);
            Union();
        }
        if(num == 150)
        {
            sorce+=1000;
        }
        printf("Final score: %d, with %d balls remaining. \n",sorce,150-num);
    }
    return 0;
}
相關文章
相關標籤/搜索