Description:
eight-way block mobile games require starting from the starting state of a matrix of 8 numbers (represented by 1-8) and a space block (represented by 0, move the space block to swap it with the adjacent block until the defined target state is reached. The space blocks can be moved from top to bottom, left to right in the middle. There are two directions on the four corners, and three directions on the other. For example, assume that the initial status of a 3x3 matrix is:
8 0 3
2 1 4
7 6 5
the target status is:
1 2 3
8 0 4
7 6 5
A valid mobile path is:
8 0 3 8 1 3 1 3 0 1 3 1 0 3 1 2 3
2 1 4 => 2 0 4 => 0 2 4 => 8 2 4 => 8 2 4 => 8 0 4
7 6 5 7 6 5 6 5 5 6 5 5 5 5 5 5 5 5 5 5 5 5 5
In addition, among all possible moving paths from the initial state to the target State, the path with the least number of steps is called the shortest path. In the above example, the shortest path is 5. If there is no path from the initial test status to the target status, the status of the group is not resolved.
Please design it effectively (For details, refer to the scoring rules)AlgorithmFind the shortest path from the initial test status of the eight parts to the target status, and use C/C ++.
Input data:
ProgramRead the target State of start.txt and goal.txt. These two files are composed of nine numbers (0 indicates space, 1-8 indicates 8 digit blocks). Each row has three numbers separated by spaces.
Output Data:
If the input data is parsed, a non-negative integer indicating the shortest path is output. If the input data is not parsed,-1 is output.
Test cases:
If the input is: start.txt:
8 0 3
2 1 4
7 6 5
And goal.txt:
1 2 3
8 0 4
7 6 5
The output is as follows:
5
Another example:
7 8 4
3 5 6
1 0 2
Replace the content in start.txt with the following output:
21
Scoring rules:
1. We will first use the same goal.txt as the self-testing use case with 10 start.txt. The running time of each test case should not exceed 10 seconds on an Intel Xeon 2.80 GHz 4 CPU/6g memory Linux machine (memory usage is not limited ), otherwise, the use case does not score;
2) total score of each contestant (accurate to 6 digits after the decimal point) = The number of test cases that can generate the correct results within 10 seconds x 10 + (1/the average running milliseconds of the test cases that generate the correct results );
3) if the number of winners of the first, second, and third prizes that will be determined in the finals cannot be calculated based on this score, we will set n = 2 first, repeat the following process until the highest 9-digit score is generated: Use the random generation of another 10 release start.txt to perform the test, and use the formula 2) for the 10 * n test cases to recalculate the total score, N ++.