2021-11-10

从两个袋子里摸球

详情请见[蓝桥杯2016决赛]随意组合。
相当于两个袋子,一个袋子装着序号为2 3 5 8的球,另一个1 4 6 7。每次从两个袋子分别拿一个球,一共拿四次。
第一次拿可能的组合21 24 26 27或者31 34 36 37或者51 54 56 57。。。。。。。以此类推。
第二次从剩下的球拿,用的是同样的方法,就需要用递归了。详情看代码。
不考虑顺序的话会有重复所以最后需要去重

 static void bfs(array a[], array b[], int c, List<int[]>lists, List<Integer>list, Map<String,Integer>hashmap){
        if (c==4)
        {
            int t[]=new int[4];
            for (int ti=0;ti<4;ti++)
            {
                t[ti]=list.get(ti);
            }
            Arrays.sort(t);
            String s="";
            for (int ti=0;ti<4;ti++)
            {
                s+=t[ti];
            }
            if (hashmap.get(s)==null) {
                hashmap.put(s,0);
                lists.add(t);
            }
        }
        if (c<4)
        {
            for (int i=0;i<4;i++)
            {
                if (a[i].mark==0)
                {
                    for (int j=0;j<4;j++)
                    {
                        if (b[j].mark==0)
                        {
                            list.add(a[i].num*10+b[j].num);
                            a[i].mark=1;b[j].mark=1;
                            bfs(a,b,c+1,lists,list,hashmap);
                            list.remove(list.size()-1);
                            a[i].mark=0;b[j].mark=0;
                        }
                    }
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值