gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:48:50 25 4
gpt4 key购买 nike

我收到了一些编程任务,需要完成这些任务才能进入工作面试的下一步。让他们都接受期待一个,描述: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;
}

}

}

最佳答案

不知道你是否明白了。

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

关于java - PebbleSolitaire 解决方案未在 Kattis (java) 中被接受,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37752417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com