使用DFS跳青蛙(青蛙拼图)

时间:2016-01-13 23:43:50

标签: java

我一直在尝试在Java中实现Jumping Frogs拼图(这里是我的意思http://britton.disted.camosun.bc.ca/frog_puzzle.htm的一个很好的链接),其目标是打印从开头到右边结束的步骤(成功的解决方案) )。 我正在使用Arraylist来编写我已经访问过的节点,并在必要时使用堆栈来返回父节点。 然而,该程序打印奇怪的东西,虽然算法似乎工作正常...任何想法为什么会发生这种情况,我该如何解决这个问题?

public class JumpingFrogs {

public static int indexOfZero(int arr[]){
    int index=0;
    for (int i=0; i <arr.length; i++){
        if (arr[i]==0) index = i;
    }
    return index;
}

public static int[] swap(int[] arr, int ind1, int ind2){
    int temp = arr[ind1];
    arr[ind1] = arr[ind2];
    arr[ind2] = temp;
    return arr;
}

public static int[] makeCopy(int[] arr){
    int[] copy = new int[arr.length];
    for (int i = 0; i < copy.length; i++){
        copy[i] = arr[i];
    }
    return copy;
}

public static boolean isContained(int[] arr, ArrayList arrayList){
    if(arrayList.isEmpty()) return false;
    for (Object arrayList1 : arrayList) {
        if (Arrays.equals(arr, (int[]) arrayList1)) {
            return true;
        }
    }
    return false;
}

public static void main(String[] args) {

    System.out.print("Input number of frogs on each side: ");
    Scanner input = new Scanner(System.in);
    int frogNumber = input.nextInt();
    int[] root = new int[2*frogNumber + 1];
    int[] solution = new int[2*frogNumber + 1];
    for (int i = 0; i < 2*frogNumber + 1; i++){
        if (i < frogNumber){
          root[i] = 1;  
          solution[i]=2;
        }
        else{
            if (i == frogNumber){
                root[i] = 0;
                solution[i] = 0;
            }
            else{
                root[i] = 2;
                solution[i] = 1;
            }
        }
    }

    Stack stack = new Stack();
    ArrayList visitedNodes = new ArrayList();
    stack.push(makeCopy(root));


    do{

        int i0=indexOfZero(root);
        if((i0 >= 2)&&(root[i0-2] == 1)&&(!isContained((swap(makeCopy(root), i0 - 2, i0)), visitedNodes))){
            swap(root, i0 - 2, i0); 
            stack.push(makeCopy(root));
            visitedNodes.add(makeCopy(root));    
            continue;
        }
        if((i0 >= 1)&&(root[i0-1] == 1)&&(!isContained((swap(makeCopy(root), i0 - 1, i0)), visitedNodes))){
            swap(root, i0 - 1, i0);
            stack.push(makeCopy(root));
            visitedNodes.add(makeCopy(root));
            continue;
        }
        if((i0 < root.length - 1)&&(root[i0+1] == 2)&&(!isContained((swap(makeCopy(root), i0+1, i0)), visitedNodes))){
            swap(root, i0 + 1, i0);
            stack.push(makeCopy(root));
            visitedNodes.add(makeCopy(root));
            continue;
        }
        if((i0 < root.length - 2)&&(root[i0+2] == 2)&&(!isContained((swap(makeCopy(root), i0+2, i0)), visitedNodes))){
            swap(root, i0 + 2, i0);
            stack.push(makeCopy(root));
            visitedNodes.add(makeCopy(root));
            continue;
        }

        stack.pop();
        root=(int[]) stack.peek();
    }
    while(!Arrays.equals(root, solution));

    Stack path = new Stack();
    while(!stack.empty()){
        path.push(stack.pop());
    }

    while(!path.empty()){
        int[] step = (int[]) path.pop();
        for (int p = 0; p < step.length; p++){
            System.out.print(step[p]);
        }
        System.out.println();
    }

    System.out.println("The program has ended successfully!");



}

}

1 个答案:

答案 0 :(得分:0)

尝试克隆 root 之类的,

do{
    proccessing
     .
     .
     .
     .
     .
     .         
    stack.pop();
    root=(int[]) stack.peek();
    //add
    root = root.clone();
  }