POJ 1027 The Same Game

题目大意:

在一个10x15的方阵里填满了RGB三种小球,要求:

1、找到最大的一片相同颜色区域,并删掉,(这片区域相同颜色球必须相邻,且相邻数大于1)。

2、删除完成后,其他小球自然下落填充。先每列向下填充,空的列由右边一列平移补全。

3、当区域内没有球或最大相邻区域的个数为一,游戏结束。


模拟加BFS,很简单,下面是代码:

#include <stdio.h>
#include <string.h>
char m[10][16];
bool vist[10][15];
struct node
{
    int x,y;
};
void Refresh()
{
    bool vis[15]= {0};
    int i,j;
    for(j=0; j<15; j++)
    {
        bool flag=false;
        int pi=-1;
        for(i=0; i<10; i++)
        {
            if(m[i][j])
            {
                flag=true;
                if(pi!=-1)
                {
                    m[pi][j]=m[i][j];
                    m[i][j]=0;
                    i=pi;
                    pi=-1;
                }
            }
            else
            {
                pi=i;
                while(i+1<10 && !m[i+1][j])
                {
                    i++;
                }
            }
        }
        if(!flag)
        {
            vis[j]=true;
        }
    }
    int k=-1;
    for(j=0; j<15; j++)
    {
        if(!vis[j])
        {
            if(k!=-1)
            {
                for(int x=0; x<10; x++)
                {
                    m[x][k]=m[x][j];
                    m[x][j]=0;
                }
                vis[j]=true;
                j=k;
                k=-1;
            }
        }
        else
        {
            k=j;
            while(j+1<15 && vis[j+1])
            {
                j++;
            }
        }
    }
    return;
}
int maxs=-1;
char C;
int x,y;
int BFS(int i,int j)
{
    struct node qu[151];
    int head=0,tail=0;
    qu[tail].x=i;
    qu[tail].y=j;
    tail++;
    vist[i][j]=true;
    int size1=0;
    char color=m[i][j];
    while(head<tail)
    {
        int x=qu[head].x;
        int y=qu[head].y;
        head++;
        size1++;

        if(x+1<10 && !vist[x+1][y] && m[x+1][y]==color)
        {
            vist[x+1][y]=true;
            qu[tail].x=x+1;
            qu[tail].y=y;
            tail++;
        }
        if(x-1>=0 && !vist[x-1][y] && m[x-1][y]==color)
        {
            vist[x-1][y]=true;
            qu[tail].x=x-1;
            qu[tail].y=y;
            tail++;
        }
        if(y-1>=0 && !vist[x][y-1] && m[x][y-1]==color)
        {
            vist[x][y-1]=true;
            qu[tail].x=x;
            qu[tail].y=y-1;
            tail++;
        }
        if(y+1<15 && !vist[x][y+1] && m[x][y+1]==color)
        {
            vist[x][y+1]=true;
            qu[tail].x=x;
            qu[tail].y=y+1;
            tail++;
        }
    }
    return size1;
}
void Search()
{
    memset(vist,false,sizeof(vist));
    maxs=0;
    for(int j=0; j<15; j++)
        for(int i=0; i<10; i++)
        {
            int size1=0;
            if(!vist[i][j] && m[i][j])
            {
                size1=BFS(i,j);
                if(maxs<size1)
                {
                    maxs=size1;
                    x=i;
                    y=j;
                }
            }
        }
    return;
}
void Del(void)
{
    struct node qu[151];
    int head=0,tail=0;
    qu[tail].x=x;
    qu[tail].y=y;
    tail++;
    C=m[x][y];
    m[x][y]=0;
    while(head<tail)
    {
        int x=qu[head].x;
        int y=qu[head].y;
        head++;
        m[x][y]=0;

        if(x+1<10 && m[x+1][y]==C)
        {
            m[x+1][y]=0;
            qu[tail].x=x+1;
            qu[tail].y=y;
            tail++;
        }
        if(x-1>=0 && m[x-1][y]==C)
        {
            m[x-1][y]=0;
            qu[tail].x=x-1;
            qu[tail].y=y;
            tail++;
        }
        if(y-1>=0 && m[x][y-1]==C)
        {
            m[x][y-1]=0;
            qu[tail].x=x;
            qu[tail].y=y-1;
            tail++;
        }
        if(y+1<15 && m[x][y+1]==C )
        {
            m[x][y+1]=0;
            qu[tail].x=x;
            qu[tail].y=y+1;
            tail++;
        }
    }
    return;
}
int main()
{
    int T,i,cnt=0;
    scanf("%d",&T);
    while(T--)
    {
        cnt++;
        for(i=9; i>=0; i--)
        {
            scanf("%s",m[i]);
        }
        int step=0;
        int ballnum=150;
        int Score=0;
        printf("Game %d: \n\n",cnt);
        while(1)
        {
            maxs=-1;
            Search();
            if(maxs==0 || maxs==1)
            {
                break;
            }
            Del();
            Refresh();
            int score=(maxs-2)*(maxs-2);
            printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n",step+1,x+1,y+1,maxs,C,score);
            ballnum-=maxs;
            Score+=score;
            step++;
        }
        if(ballnum==0)
        {
            Score+=1000;
        }
        printf("Final score: %d, with %d balls remaining.\n\n",Score,ballnum);
    }
    return 0;
}


posted @ 2013-10-06 12:04  、小呆  阅读(240)  评论(0编辑  收藏  举报