因此,这段代码的目标是递归地移动特定单元格上带有青蛙的二维数组。一只青蛙可以跳过另一只青蛙并删除它。这段代码应该可以看到有多少种不同的方法可以将它传递给一只青蛙。但是,在找到一条路径后,原始数组被更改,它仍然只有一个来自前一条路径的青蛙。如何更改此项以使原始数组不受影响?
public void RecursiveFunc(Units[][] array) {
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array.length; y++) {
temp[x][y] = array[x][y];
}
}
//make move on temp board and recursively call again
if (CheckNumberFrogs(temp) == 1 && CheckLastID(temp) == getgoalLoc()) {
numOfPlans += 1;
}
else {
for(int i= 0; i < temp.length; i++){
for(int j = 0; j < temp.length; j++){
if (temp[i][j].Frog == true) {
//Every possible movement is done here and then recursively recalled
if (i > 1) {
//can go left
if (temp[i-1][j].Frog == true && temp[i-2][j].Frog == false) {
temp[i][j].removeFrog();
temp[i-1][j].removeFrog();
temp[i-2][j].setFrog();
RecursiveFunc(temp);
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array.length; y++) {
temp[x][y] = array[x][y];
}
}
}
}
if (i < (temp.length - 2)) {
//can go right
if (temp[i+1][j].Frog == true && temp[i+2][j].Frog == false) {
temp[i][j].removeFrog();
temp[i+1][j].removeFrog();
temp[i+2][j].setFrog();
RecursiveFunc(temp);
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array.length; y++) {
temp[x][y] = array[x][y];
}
}
}
}
if (j > 1) {
//can go down
if (temp[i][j-1].Frog == true && temp[i][j-2].Frog == false) {
temp[i][j].removeFrog();
temp[i][j-1].removeFrog();
temp[i][j-2].setFrog();
RecursiveFunc(temp);
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array.length; y++) {
temp[x][y] = array[x][y];
}
}
}
}
if(j < (temp.length - 2)) {
//can go up
if (temp[i][j+1].Frog == true && temp[i][j+2].Frog == false) {
temp[i][j].removeFrog();
temp[i][j+1].removeFrog();
temp[i][j+2].setFrog();
RecursiveFunc(temp);
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array.length; y++) {
temp[x][y] = array[x][y];
}
}
}
}
if (i > 1 && j > 1) {
//can go left and down (diagonal)
if (temp[i-1][j-1].Frog == true && temp[i-2][j-2].Frog == false) {
temp[i][j].removeFrog();
temp[i-1][j-1].removeFrog();
temp[i-2][j-2].setFrog();
RecursiveFunc(temp);
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array.length; y++) {
temp[x][y] = array[x][y];
}
}
}
}
if (i < (temp.length - 2) && j > 1) {
//can go right and down (diagonal)
if (temp[i+1][j-1].Frog == true && temp[i+2][j-2].Frog == false) {
temp[i][j].removeFrog();
temp[i+1][j-1].removeFrog();
temp[i+2][j-2].setFrog();
RecursiveFunc(temp);
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array.length; y++) {
temp[x][y] = array[x][y];
}
}
}
}
if (i > 1 && j < (temp.length - 2)) {
//can go left and up (diagonal)
if (temp[i-1][j+1].Frog == true && temp[i-2][j+2].Frog == false) {
temp[i][j].removeFrog();
temp[i-1][j+1].removeFrog();
temp[i-2][j+2].setFrog();
RecursiveFunc(temp);
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array.length; y++) {
temp[x][y] = array[x][y];
}
}
}
}
if (i < (temp.length - 2) && j < (temp.length - 2)) {
//can go right and up (diagonal)
if (temp[i+1][j+1].Frog == true && temp[i+2][j+2].Frog == false) {
temp[i][j].removeFrog();
temp[i+1][j+1].removeFrog();
temp[i+2][j+2].setFrog();
RecursiveFunc(temp);
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array.length; y++) {
temp[x][y] = array[x][y];
}
}
}
}
//end
}
}
}
}
}
x(x)
1条答案
按热度按时间qhhrdooz1#
您必须显式地创建数组的副本并存储它。
对于二维数组,这意味着您需要一个完整的循环来复制数组:
至于“为什么”,请记住对象是数组,并研究https://stackoverflow.com/a/40523/869736。