LeetCode - Zuma Game

本文介绍了一种游戏消除算法的实现方法,通过定义节点结构来存储游戏面板的状态,并使用递归深度优先搜索策略来寻找最优的操作步骤,使得玩家可以通过最少的操作达到游戏目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class Solution {
public:
    struct node{
    char fs[25];
    int  fk[25];
    int len ;
    node(){
        memset(fs,0,sizeof(fs));
        memset(fk,0,sizeof(fk));
        len = 0;
    }
};


node del(node a){
    node b;
    for(int i = 0 ; i < a.len ; i++){
        if(a.fk[i]>=3){
            continue;
        }
        if(b.len>0&&b.fs[b.len-1]==a.fs[i]){
            b.fk[b.len-1]+=a.fk[i];
            continue;
        }
        b.fs[b.len]=a.fs[i];
        b.fk[b.len]=a.fk[i];
        b.len++;
    }
    if(b.len!=a.len)return del(b);
    return b;
}
int vmin ;

void fuck(node a){
    for(int i = 0 ; i < a.len ; i ++){
        cout<<a.fs[i]<<a.fk[i]<<" ";
    }
    cout<<endl;
}

void dfs(node a,string hand,int step){
   // fuck(a);
    a = del(a);
  //  fuck(a);
  //  getchar();
    if(a.len == 0){
        if(vmin > step){
            vmin = step;
        }
        return ;
    }
    for(int i = 0 ; i < a.len ;i ++){
        for(int j = 0 ; hand[j] ; j ++){
            if(hand[j]=='!')continue;
            if(hand[j]==a.fs[i]){
                a.fk[i]++;
                hand[j]='!';
                dfs(a,hand,step+1);
                hand[j]=a.fs[i];
                a.fk[i]--;
            }
        }
    }
}


int findMinStep(string board, string hand) {
        vmin = 999;

        node a;
        for(int i = 0 ; board[i] ; i ++){
            if(a.len>0 && a.fs[a.len-1]== board[i]){
                a.fk[a.len-1]++;
                continue;
            }
            a.fs[a.len] = board[i];
            a.fk[a.len] = 1;
            a.len++;
        }

        dfs(a,hand,0);
        if(vmin!=999){
            return vmin;
        }
        return -1;
}
};

转载于:https://www.cnblogs.com/clover-xuqi/p/7154866.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值