# The circled number in the java initial square is a marker maze that can be moved to other squares along the line

Keywords: Mobile

The circled numbers in the initial square are markers that can be moved to other squares along the line At each step of the puzzle, you can move the number of squares indicated by the currently occupied integer in the marked square The tag can move left or right along the line, but it can go through either end without moving For example, the only legal first step is to move the tag three squares to the right, because there is no space to move three spaces to the left.

The goal of the puzzle is to move the tag to 0 at the far end of the row.

Requirement: program with recursion or stack or queue.
Returns true if the circle can move to the last 0, otherwise false.
The program needs to adapt to the number of squares of any size, and the numbers in the squares are random, and the range is [1,9].
How to detect the unsolvable structure and whether there is a way to improve the execution time.
In order to run the program, it is necessary to input the random number of squares, print out the squares, and then print the detailed moving path. ```public class PuzzleService {
public static void main(String[] args) {
// This ar array is the number on the topic
//int[] ar = {4,8,5,2,4,5,1,6,1,0};

// Here is a randomly generated ar array
int[] ar = new int;
Random rd = new Random();
for (int i = 0; i < 9; i++) {
ar[i] = rd.nextInt(9)+1;
}
ar = 0;

int[] x = new int;// x is an array that records the information of the moving path
Arrays.fill(x, -1); // Initialize all element positions of the array to 0
// Find path mobile information
if(!func(ar, 0, x)){
System.out.println("No path exists");
} else {
// Print path information
backstrace(x, ar, ar.length-1);
}
// The following two lines are auxiliary printing information
System.out.println(Arrays.toString(ar));
System.out.println(Arrays.toString(x));
}

/**
* Print the final path mobile information
* @param x
* @param ar
* @param index
*/
private static void backstrace(int[] x, int[] ar, int index) {
if(index == 0) {
System.out.println("Array location:" + index + "number:" + ar[index]);
return;
}
backstrace(x, ar, x[index]);
System.out.println("Array location:" + index + "number:" + ar[index]);
}

/**
* Recursive implementation to find the path information of puzzle
* @param ar
* @param i
* @param x: Mobile information of storage path
* @return: Find the path information and return true, otherwise return false
*/
private static boolean func(int[] ar, int i, int[] x) {
// End of recursion, find the last element 0
if(i == ar.length-1){
return true;
}

// Go to the right of the current position
if(i+ar[i] <= ar.length-1){
if(x[i+ar[i]] == i){ // It means that you have passed this position, and return to false directly
return false;
}
x[i+ar[i]] = i;
if(func(ar, i+ar[i], x)){
return true;
}
}

// Go to the left of the current position
if(i-ar[i] >= 0){
if(x[i-ar[i]] == i){ // It means that you have passed this position, and return to false directly
return false;
}
x[i-ar[i]] = i;
if(func(ar, i-ar[i], x)){
return true;
}
}

return false; // It means that the current position i is unable to walk to both sides, return to false
}
}

```

Posted by zyrolasting on Mon, 04 Nov 2019 08:33:23 -0800