POJ 1027 The Same Game BFS

本文介绍了一个基于宽度优先搜索(BFS)算法的游戏实现案例。游戏中玩家通过消除相邻颜色相同的球来获得分数,重点介绍了如何使用BFS算法寻找并标记可消除的球群,以及消除后如何调整游戏版面。

没有什么技术含量的BFS,因为在数量相同的情况下要优先选择左边的,其次是下面的。

所以找的时候要从最左边的一列开始,每一列从下面开始。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>

using namespace std;

struct N
{
   int x,y,clu;
}re;
struct Q
{
    int r,c;
}t1,t2;

queue<Q> q;

char cb[15][20];
bool MarkVisit[15][20];

int bfs(int r,int c)
{
    int jr[] = {-1, 0, 1, 0};
    int jc[] = { 0 ,1, 0,-1};

    int i,ans = 1;

    t1.r = r;
    t1.c = c;
    q.push(t1);

    MarkVisit[r][c] = true;

    while(q.empty() == false)
    {
        t1 = q.front();
        q.pop();
        for(i = 0;i < 4; ++i)
        {
            t2.r = t1.r + jr[i];
            t2.c = t1.c + jc[i];
            if(1 <= t2.r && t2.r <= 10 && 1 <= t1.c && t1.c <= 15 && MarkVisit[t2.r][t2.c] == false && cb[t2.r][t2.c] == cb[t1.r][t1.c])
            {
                q.push(t2);
                MarkVisit[t2.r][t2.c] = true;
                ans ++;
            }
        }
    }
    return ans;
}

void delcb(int r,int c)
{
    memset(MarkVisit,false,sizeof(MarkVisit));

    int jr[] = {-1, 0, 1, 0};
    int jc[] = { 0 ,1, 0,-1};

    int i,j,k;

    t1.r = r;
    t1.c = c;
    q.push(t1);

    MarkVisit[r][c] = true;

    while(q.empty() == false)
    {
        t1 = q.front();
        q.pop();
        for(i = 0;i < 4; ++i)
        {
            t2.r = t1.r + jr[i];
            t2.c = t1.c + jc[i];
            if(1 <= t2.r && t2.r <= 10 && 1 <= t1.c && t1.c <= 15 && MarkVisit[t2.r][t2.c] == false && cb[t2.r][t2.c] == cb[t1.r][t1.c])
            {
                q.push(t2);
                MarkVisit[t2.r][t2.c] = true;

            }
        }
    }

    for(i = 1;i <= 10; ++i)
    {
        for(j = 1;j <= 15; ++j)
        {
            if(MarkVisit[i][j])
                cb[i][j] = 'a';
        }
    }

    for(i = 1;i <= 15; ++i)
    {
        for(k = 0;k <= 10; ++k)
        {
            for(j = 1;j <= 10; ++j)
            {
                if(cb[j][i] == 'a')
                {
                    if(j != 10)
                    {
                        cb[j][i] = cb[j+1][i];
                        cb[j+1][i] = 'a';
                    }
                }
            }
        }
    }

    for(k = 1 ; k <= 15; ++k)
    {
        for(i = 1;i <= 15; ++i)
        {
            for(j = 1;j <= 10; ++j)
            {
                if(cb[j][i] != 'a')
                    break;
            }
            if(j == 11)
            {
                if(i != 15)
                {
                    for(j = 1;j <= 10; ++j)
                    {
                        cb[j][i] = cb[j][i+1];
                        cb[j][i+1] = 'a';
                    }
                }
            }
        }
    }
}

int main()
{
    int T,icase = 0;
    int i,j,ans,step;
    int sumdel,getpoint,del;
    scanf("%d",&T);
    while(T--)
    {
        step = 0;
        sumdel = 0;
        getpoint = 0;
        del = 0;

        for(i = 10;i >= 1 ; --i)
            cin>>(cb[i]+1);

        printf("Game %d:\n\n",++icase);
        while(1)
        {
            re.x = -1;
            re.y = -1;
            re.clu = -1;

            memset(MarkVisit,false,sizeof(MarkVisit));

            for(j = 1;j <= 15; ++j)
            {
                for(i = 1;i <= 10; ++i)
                {
                    if(MarkVisit[i][j] == false && cb[i][j] != 'a')
                    {
                        ans = bfs(i,j);
                        if(ans > re.clu)
                        {
                            re.x = i;
                            re.y = j;
                            re.clu = ans;
                        }
                        else if(ans == re.clu)
                        {
                            if(j < re.y)
                            {
                                re.x = i;
                                re.y = j;
                                re.clu = ans;
                            }
                            else if(j == re.y)
                            {
                                if(i < re.x)
                                {
                                    re.x = i;
                                    re.y = j;
                                    re.clu = ans;
                                }
                            }
                        }
                    }
                }
            }
            if(re.clu < 2)
                break;

            getpoint += (re.clu-2)*(re.clu-2);
            sumdel += re.clu;

            printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n",++step,re.x,re.y,re.clu,cb[re.x][re.y],(re.clu-2)*(re.clu-2));
            delcb(re.x,re.y);
        }
        if(sumdel == 150)
        {
            getpoint += 1000;
        }
        printf("Final score: %d, with %d balls remaining.\n\n",getpoint,150-sumdel);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值