PebbleSolitaire解决方案未被Kattis接受(java)

时间:2016-06-10 15:51:46

标签: java recursion

我接受了一些编程任务,以便进入求职面试的下一步。他们都接受了一个,描述:https://academicwork.kattis.com/problems/pebblesolitaire2(自动代码校正软件)

虽然我使用递归已经很长时间了,但我仍然认为我想出了一个非常简单且有效的解决方案。 (可能不是最好的。)它处理问题描述中提供的所有“样本输入”,并根据我的意见给出正确的“输出”。它仍然被拒绝,唯一的拒绝线索是“在测试文件2/7它失败了:错误答案”。没有更多的事情继续下去,我真的无法弄清楚我的代码在哪里给出了错误的答案。

有关如何前进的任何建议?

import java.util.Scanner;
public class PebbleSolitaire {

private static int bestMove;
private static char[] table;

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    while (scan.hasNext()) {
        int i = scan.nextInt();

        for (int j = 0; j < i; j++) {
            bestMove = 23;
            String a = scan.next();
            table = a.toCharArray();
            char[] testTable = a.toCharArray();
            checkMoves(testTable);
            System.out.println(bestMove);

        }

    }

}

public static void checkMoves(char[] array) {

    for (int i = 0; i < 22; i++) {
        if (array[i] == 'o' && array[i + 1] == 'o') {
            if (i + 2 < 23 && array[i + 2] == '-' && i - 1 >= 0 && array[i - 1] == '-') {
                char[] tempArray;
                tempArray = array;
                tempArray[i - 1] = 'o';
                tempArray[i] = '-';
                tempArray[i + 1] = '-';
                checkMoves(tempArray);

                table[i + 2] = 'o';
                table[i] = '-';
                table[i + 1] = '-';
                checkMoves(table);

            }
            if (i + 2 < 23 && array[i + 2] == '-') {
                array[i + 2] = 'o';
                array[i] = '-';
                array[i + 1] = '-';
                checkMoves(array);
            }
            if (i - 1 >= 0 && array[i - 1] == '-') {
                array[i - 1] = 'o';
                array[i] = '-';
                array[i + 1] = '-';
                checkMoves(array);
            }
        }
    }

    int counter = 0;

    for (int i = 0; i < 23; i++) {
        if (array[i] == 'o') {
            counter++;

        }
    }

    if (counter < bestMove) {
        bestMove = counter;
    }

}

}

1 个答案:

答案 0 :(得分:1)

不知道你是否已经弄清楚了。

问题是您为数组创建的变量是指SAME对象。因此,每当您更改任何变量(无论是array还是tempArray)时, OBJECT 的值都会发生变化。数组变量引用相同的对象。