蓝桥杯题解 跳蚱蜢 Java答案 附运算过程

原题链接
题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

如下图所示: 有 99 只盘子,排成 11 个圆圈。 其中 88 只盘子内装着 88 只蚱蜢,有一个是空盘。 我们把这些蚱蜢顺时针编号为 11 ~ 88。

每只蚱蜢都可以跳到相邻的空盘中, 也可以再用点力,越过一个相邻的蚱蜢跳到空盘中。

请你计算一下,如果要使得蚱蜢们的队形改为按照逆时针排列, 并且保持空盘的位置不变(也就是 1-81−8 换位,2-72−7换位,…),至少要经过多少次跳跃?

解答代码:

public class Main {

    static int d[] = {1,2,3,4,5,6,7,8,0};

    static int ans[] = {8,7,6,5,4,3,2,1,0};

    static ArrayList<Integer> aans;

    static Queue<cls> q = new LinkedList<>();

    static HashMap<ArrayList<Integer>, Integer> map = new HashMap();

    static int ansStep = 0;

    static class cls {
        public ArrayList<Integer> q;
        public int step = 0;
        public cls() {
            q = new ArrayList<>();
        }
    }

    public static void main(String[] args) throws Exception {
        ArrayList<Integer> l = arrayToList(d);
        aans = arrayToList(ans);
        map.put(arrayToList(d), 1);
        cls c = new cls();
        c.q = l;
        q.add(c);
        bfs();
        System.out.println("ans:" + ansStep);
    }

    static void bfs() throws InterruptedException {
        while(!q.isEmpty()) {
            cls c = q.poll();
            if(c.q.equals(aans)) {
                ansStep = c.step;
                break;
            }
            int t1 = -1;
            for(int i = 0;i < 9;i ++ ) {
                if(c.q.get(i) == 0) {
                    t1 = i;
                    break;
                }
            }
            if(t1 != -1) {
                {
                    ArrayList<Integer> l1 = (ArrayList<Integer>) c.q.clone();
                    reduceone(l1, t1);
                    if(!map.containsKey(l1)) {
                        System.out.println("swap:" + t1 + " " + (t1 - 1));
                        map.put(l1, 1);
                        System.out.println(l1.toString() + " " + (c.step + 1));
                        cls cc = new cls();
                        cc.step = c.step + 1;
                        cc.q = l1;
                        q.add(cc);
                    }
                }
                {
                    ArrayList<Integer> l1 = (ArrayList<Integer>) c.q.clone();
                    reducetwo(l1, t1);
                    if(!map.containsKey(l1)) {
                        System.out.println("swap:" + t1 + " " + (t1 - 2));
                        map.put(l1, 1);
                        System.out.println(l1.toString() + " " + (c.step + 1));
                        cls cc = new cls();
                        cc.step = c.step + 1;
                        cc.q = l1;
                        q.add(cc);
                    }
                }
                {

                    ArrayList<Integer> l1 = (ArrayList<Integer>) c.q.clone();
                    addone(l1, t1);
                    if(!map.containsKey(l1)) {
                        System.out.println("swap:" + t1 + " " + (t1 + 1));
                        System.out.println("21312312");
                        map.put(l1, 1);
                        System.out.println(l1.toString() + " " + (c.step + 1));
                        cls cc = new cls();
                        cc.step = c.step + 1;
                        cc.q = l1;
                        q.add(cc);
                    }
                }
                {
                    ArrayList<Integer> l1 = (ArrayList<Integer>) c.q.clone();
                    addtwo(l1, t1);
                    if(!map.containsKey(l1)) {
                        System.out.println("swap:" + t1 + " " + (t1 + 2));
                        map.put(l1, 1);
                        System.out.println(l1.toString() + " " + (c.step + 1));
                        cls cc = new cls();
                        cc.step = c.step + 1;
                        cc.q = l1;
                        q.add(cc);
                    }
                }
            }
        }
    }

    static void addone(ArrayList<Integer> d, int i) {
        int t = (i + 1 + 9) % 9;
        int tt = d.get(i);
        d.set(i, d.get(t));
        d.set(t, tt);
    }

    static void addtwo(ArrayList<Integer> d, int i) {
        int t = (i + 2 + 9) % 9;
        int tt = d.get(i);
        d.set(i, d.get(t));
        d.set(t, tt);
    }

    static void reduceone(ArrayList<Integer> d, int i) {
        int t = (i - 1 + 9) % 9;
        int tt = d.get(i);
        d.set(i, d.get(t));
        d.set(t, tt);
    }

    static void reducetwo(ArrayList<Integer> d, int i) {
        int t = (i - 2 + 9) % 9;
        int tt = d.get(i);
        d.set(i, d.get(t));
        d.set(t, tt);
    }

    static ArrayList<Integer> arrayToList(int[] array) {
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0;i < array.length;i ++ ) {
            list.add(array[i]);
        }
        return list;
    }

最终答案:20

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值