首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >广度优先搜索8瓦片游戏

广度优先搜索8瓦片游戏
EN

Stack Overflow用户
提问于 2017-04-29 05:46:13
回答 1查看 197关注 0票数 0

我创建了一个广度优先搜索为基础的8瓦片游戏。让我解释一下这个游戏:

您将看到一个包含8个整数的数组:

代码语言:javascript
复制
012
348
675

和被赋予一个整数m,它告诉你哪个瓦片是空的,在这个例子中,假设它是8。你只能将这个瓦片与它的正上方、右下方的瓦片交换,使用这种方法,你需要对数组进行排序,如下所示:

代码语言:javascript
复制
012
345
678

您需要将空格中的步长放入一个向量中。因此,在上面的示例中,向量应该是5,8,因为在排序时,空格开始于pos#5,结束于pos#8。我创建了用于排序的代码,现在我不知道如何将位置添加到向量中,以下是我的代码:

代码语言:javascript
复制
package application;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Vector;

public class Solution {

    /******************************************
     * Implementation Here
     ***************************************/

    /*
     * Implementation here: you need to implement the Breadth First Search
     * Method
     */
    /* Please refer the instruction document for this function in details */

    public static LinkedHashSet<int[]> OPEN = new LinkedHashSet<int[]>();
    public static HashSet<int[]> CLOSED = new HashSet<int[]>();
    public static boolean STATE = false;
    public static int empty;

    public static void breadthFirstSearch(int[] num, int m, Vector solution1) {

        //int[] start = num;
        int[] start = {0,1,2,3,4,5,6,8,7};
        m = 7;
        int[] goal = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
        int[] X;
        int[] temp = {};

        OPEN.add(start);

        while (OPEN.isEmpty() == false) {


            X = OPEN.iterator().next();
            OPEN.remove(X);

            for (int i = 0; i < X.length; i++) {
                if (X[i] == m) {
                    empty = i;
                    System.out.println("empty = " + empty);
                    solution1.addElement(empty);
                }
            }

            // get position of ZERO or EMPTY SPACE
            if (compareArray(X, goal)) {


                System.out.println("SUCCESS");
                for(Object i : solution1) {
                    System.out.println((int)i);
                }
                print(X);
                break;
            } else {


                //print(X);
                System.out.println("------");

                // generate child nodes
                CLOSED.add(X);
                int[][] y = new int[4][9];
                for (int i = 0; i < 4; i++)
                    for (int i1 = 0; i1 < 9; i1++)
                        y[i][i1] = X[i1];

                temp = up(y[0], empty);
                if (temp != null) {
                    OPEN.add(temp);
                    System.out.println("doing up");

                    System.out.println("added "+ empty);
                //  print(temp);

                }

                temp = left(y[1], empty);

                if (temp != null) {
                    OPEN.add(temp);
                    System.out.println("doing left");

                    System.out.println("added "+ empty);
            //      print(temp);
                }

                temp = down(y[2], empty);
                if (temp != null) {
                    OPEN.add(temp);
                    System.out.println("doing down");

                    System.out.println("added "+ empty);
            //      print(temp);
                }
                temp = right(y[3], empty);
                if (temp != null) {
                    OPEN.add(temp);
                    System.out.println("doing right");

                    System.out.println("added "+ empty);
            //      print(temp);

                }
                if (OPEN.isEmpty()) {
                    // System.out.println("Ending loop");
                }
            }
        }

    }

    public static void print(int[] arr) {
        System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
        System.out.println(arr[3] + " " + arr[4] + " " + arr[5]);
        System.out.println(arr[6] + " " + arr[7] + " " + arr[8]);
        System.out.println("---------");

    }

    public static boolean compareArray(int[] a, int[] b) {

        for (int i = 0; i < a.length; i++)
            if (a[i] != i)
                return false;

        return true;

    }

    public static int[] up(int[] s, int p) {
        boolean b = false;
        int[] str = s;
        if (p > 3) {
            int temp = str[p - 3];
            str[p - 3] = str[p];
            str[p] = temp;
            b = true;

        }
        // Eliminates child of X if its on OPEN or CLOSED
        int[] t = new int[9];

        for (int i = 0; i < str.length; i++)
            t[i] = str[i];

        if (!OPEN.contains(t) && CLOSED.contains(t) == false && b)
            return str;
        else {
            System.out.println("returning null for up");
            return null;
        }
    }

    public static int[] down(int[] s, int p) {

        int[] str = s;
        boolean b = false;
        if (p < 6) {
            int temp = str[p + 3];
            str[p + 3] = str[p];
            str[p] = temp;

            b = true;
        }
        int[] t = new int[9];
        for (int i = 0; i < str.length; i++)
            t[i] = str[i];

        if (!OPEN.contains(str) && CLOSED.contains(str) == false && (b))
            return str;
        else {
            System.out.println("returning null for down");
            return null;
        }
    }

    public static int[] left(int[] s, int p) {
        int[] str = s;
        boolean b = false;
        if (p != 0 && p != 3 && p != 6) {
            int temp = str[p - 1];
            str[p - 1] = str[p];
            str[p] = temp;
            b = true;
        }
        // Eliminates child of X if its on OPEN or CLOSED
        int[] t = new int[9];
        for (int i = 0; i < str.length; i++)
            t[i] = str[i];

        if (!OPEN.contains(str) && CLOSED.contains(str) == false && (b))
            return str;
        else {
            System.out.println("returning null for left");
            return null;
        }
    }

    public static int[] right(int[] s, int p) {
        boolean b = false;
        int[] str = s;
        if (p != 2 && p != 5 && p != 8) {
            int temp = str[p + 1];
            str[p + 1] = str[p];
            str[p] = temp;
            b = true;
        }
        // Eliminates child of X if its on OPEN or CLOSED
        int[] t = new int[9];
        for (int i = 0; i < str.length; i++)
            t[i] = str[i];

        if (!OPEN.contains(t) && CLOSED.contains(t) == false && (b))
            return str;
        else {
            System.out.println("returning null for right");
            return null;
        }
    }

}
EN

回答 1

Stack Overflow用户

发布于 2017-04-29 16:46:53

此代码与我最近回答的代码完全相同,请检查this并相应地更新您的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43689341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档