1. Title Description: Click to open the link

2. Problem-Solving ideas: the use of iterative deepening search solution. Haven't done this topic for a long time, feel this method is a bit powerless, will not look for valuation function is mishap ... I had to learn someone else's code.

The 8 squares in the middle of the board are required to have the same number. Purple Book said is the use of state space search solution, rough template or eight digital problem template. But wrote a half-day finally WA, feel the problem with the template written out will be very complex. Finally look at other people's code, only to find that most of the use of iterative deepening search solution, the code is not very large. So this is the way to learn.

The movement of the subject is more complicated, so the position of the a,b,c,d Four directions should be saved beforehand, and the latter four directions can be rolled back. Also save the position of the 8 squares you want to observe. Next, determine if it is the target State, and if not, iterate to deepen the search.

The first step is to understand how the iterative deepening search is done: from small to large, enumerate the depth upper limit maxd. For each depth limit, DFS, when recursive to Maxd, it is necessary to determine whether to find the understanding, if found, the record or output solution. Whether or not you find it, start the return, and for the information you find, you can return continuously at the exit of the function.

So how do you get back in time? The answer is to look for "valuation functions" and "pruning". For the subject, it can be found that each move can only get the target number one more, so you can Min{diff (1), diff (2), diff (3)} as a stock function. where diff (i) is not equal to the number I in the target lattice. In this way, if you find that the minimum number of steps to move h () is still greater than maxd-cur, pruning is required.

It is important to note that iterative deepening search must be used in situations where the solution must exist, otherwise infinite recursion will occur.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace Std;int maxd, Ok;int A[50];char ans[1000]; int Line[8][7] = {{0, 2, 6, one, ten, Ten},//a{1, 3, 8,,, 9, 8, 7, 6, 5, 4},//c{19, 18 , +, 5, 4, +/-d};const int rev[8] = {, 7, 6, 1, 0, 3, 2};//through a,b,c,d, reverse other directions. Point to reverse, used to restore the scene. const int Final[8] = {6, 7, 8, 11, 12, 15, 16, 17};//The last answer requires the point bool Is_final () {for (int i = 1; i < 8; i++) if (a[final[i ]] = a[final[0]]) return False;return true; End State int diff (int k) {int ans = 0;for (int i = 0; i < 8; i++) if (a[final[i]]! = k) Ans++;return ans;} int h () {//Valuation function return min (diff (1), diff (2)), diff (3));} VoID move (int k)//move in direction {int tmp = a[line[k][0]];for (int i = 0; i < 6; i++) a[line[k][i]] = a[line[k][i + 1]];a[line[k][6 ]] = tmp;} void dfs (int cur) {if (is_final ()) {ans[cur] = ' ';p rintf ("%s\n", ans); ok = 1;return;} if (cur + h () > Maxd) return;//pruning for (int i = 0; i < 8; i++) {ans[cur] = ' A ' + i;move (i);d FS (cur + 1); if (OK) return; Move (Rev[i]);//restore Field}}void init ()//reverse direction {for (int i = 4; i < 8; i++) for (int j = 0; J < 7; J + +) Line[i][j] = Line[rev[i ]][6-J];} int main () {//freopen ("T.txt", "R", stdin), Init (), while (scanf ("%d", &a[0]) = = 1 && a[0]) {for (int i = 1; I < ; 24; i++) scanf ("%d", &a[i]), for (int i = 0; i <; i++) if (!a[i]) return 0;ok = 0;if (is_final ()) printf ("No moves Needed\n "); Elsefor (maxd = 1;; maxd++) {dfs (0); if (ok) break;} printf ("%d\n", a[6]);//Final color}return 0;}

Example 7-12 Rotating Game UVa1343