Description
Even if the number of steps in the initial state is 1, haha
Input: The first 3 * 3 matrix is the original state, and the second 3 * 3 matrix is the target state.
Output: the minimum number of steps used for movement
Input
2 8 3
1 6 4
7 0 5
1 2 3
8 0 4
7 6 5
Output
6
Reference link: Question C: [Getting started with wide search] 8 digital problems
The final AC code is as follows:
#include <bits / stdc ++. h> using namespace std; int final [ 3 ] [ 3 ]; int dir [ 4 ] [ 2 ] = {{ 0 , -1 }, { -1 , 0 }, { 0 , 1 }, { 1 , 0 }}; // Direction struct Node { int x, y, step, pdir, M [ 3 ] [ 3 ]; // pdir records the current matrix state of the last direction M } now, nex; bool judge () { inti, j; for (i = 0 ; i < 3 ; i ++ ) { for (j = 0 ; j < 3 ; j ++) if (nex.M [i] [j]! = final [i] [j]) return false ; } return true ; } int BFS () { int i, t, ti, tj; now.step = 1 ; now.pdir = 8 ; // Initialize a direction Note that the range cannot be -2 to 6! queue <Node> q; q.push (now); while (! q.empty ()) { now =q.front (); q.pop (); for (i = 0 ; i < 4 ; i ++ ) ( if (abs (i-now.pdir) == 2 ) continue ; // indicates that you want to go back to the previous one Status nex.x = now.x + dir [i] [ 0 ]; nex.y = now.y + dir [i] [ 1 ]; if (nex.x < 0 || nex.x> 2 || nex .y < 0 || nex.y> 2 ) continue ; // Unreasonable location nex.pdir = i; // Recording direction nex.step = now.step + 1 ; // step size plus 1 for (ti = 0 ; ti < 3 ; ti ++) { // copy matrix for (tj = 0 ; tj < 3 ; tj ++) nex.M (ti ] [tj] = now.M [ti] [tj]; } // Indicating that one step to the next state t = nex.M [now.x] [now.y]; nex.M [now.x] [ now.y] = nex.M [nex.x] [nex.y]; nex.M [nex.x] [nex.y] = t; if (judge ()) return nex.step; q.push ( nex); } } return -1; //别忘了遍历完,找不到的情况 } int main(){ int i, j; for(i=0; i<3; i++){ for(j=0; j<3; j++){ scanf("%d", &now.M[i][j]); if(now.M[i][j] == 0) now.x=i, now.y=j; } } for(i=0; i<3; i++){ for(j=0; j<3; j++) scanf("%d", &final[i][j]); } printf("%d\n", BFS()); return 0; }
Note: The title description is relatively simple, so a lot of important information is not given: for example, there may be cases where it does not need to return -1, and the entered 0 represents a space in the picture.
Summary: When I write, I change the state directly in the matrix, so I can't write it later. Then, referring to other people's code, I found that the method used is: the state generated by each step is stored in an array. However, it should be noted here that the direction from which the state is transformed must be recorded to avoid that the direction selected this time will return to the previous state again.
Through this question, I have a new understanding of the BFS () algorithm: during each iteration of the while loop, as long as a certain condition is met, new elements will be enqueued until the answer is found or the search is completed. Here, the process of finding answers is actually carried out in multiple ways, that is, there are some inevitable "redundant operations" before the answers are found. My own understanding is that, since I do n’t know which path can find the answer, then I will go through the first layer and then go to the second layer, and so on. The DFS () search is a typical "do not hit the south wall and do not look back".