/ * Octa Digital Problem has a 3 * 3 chessboard, which is 0-89 numbers, 0 represents spaces, other numbers can be exchanged with 0. Seeking for the initial state 1 2 34 5 67 8 0 to reach the minimum number of target status steps.
Its typical algorithm is a breadth priority search. The specific algorithm is: Struct class name m_ar [possibility node]; int h, rmain () {h = 0; r = 1; while ((h *********************************** Do I know what kind of situation, what The situation is inelease. Function f (s) represents the number of numbers than s, for S, for example: | 1 3 4 || 2 8 6 || 5 7 | Representation: | 1 3 4 | 2 8 6 | 5 7 X |, f (7) = 6, f (5) = 4, f (6) = 4, f (8) = 4, f (2) = 1, f (4) = 2, f (3) = 1, f (1) = 0 When F (A8) f (A7) ... f (A1) is an even number, it can be rejected, so that the above is solved. I will prove it below. Set up any case: | A1 A2 A3 || A4 A5 A6 || A7 A8 X | (x Representation) Place it on a row: | A1 A2 A3 | A4 A5 A6 | A7 A8 X | The up and down movement of the numbers can be moved relative to the space of the space. So we will discuss X's movement: Assuming that the function f (s) represents the number of numbers than S, the number of numbers is smashed. For example: | 1 3 4 | 2 8 6 | 5 7 x |, f (7) = 6, f (5) = 4, f (8 ) = 4, ... For the movement of X in the same row, f (A8) f (A7) ... f (A1) size is unchanged (* 1) such as: | A1 A2 A3 | A4 A5 A6 | A7 A8 X | => | A1 A2 A3 | A4 A5 A6 | A7 X A8 | For X in the column, we may wish to set up X and A6 to change (ie, a Down Movement one), the number column changes into | A1 A2 A3 | A4 A5 X | A7 A8 A6 |, may cause change f (s) only F (A6), F (A7), F (A8) Discussion: 4 cases 1) A6 In this way, then the A3 is moved down - | A1 A2 A3 | A4 A5 X | A7 A8 A6 | => | A1 A2 X | A4 A5 A3 | A7 A8 A6 | The same, for possible f (A3), F (A4), F (A5) discussed, the situation is exactly the same. Other circumstances:: CONCLAS: Because for | 1 2 3 | 4 5 6 | 7 8x |, F (8) f (7) ... f (1) = 28, is an even number, so when F (A8) f (a7) ... f (a1) is an even number to rearrange | 1 2 3 | 4 5 6 | 7 8 x | Success. * / # include Typedef unsigned long long uint64; type {CHAR X; // The number transplantation char y on the position Y; // where x is 0 position} EP_MOVE; #define size 3 // 8 Digital problem, theoretical theory can also solve 15 digital problems, # define num size * size // But Move_Gen needs to do a lot of modifications, enter the part of the initial and end status and check_input to modify #define Max_node 1000000 # Define Max_Dep 100 #define xchg (a, b) {a = a b; b = a - b; a = a - b;} #define trans (a, b) {long III; (b) = 0; for (iii = 0; III // typedf struct EP_NODE_TAG {uint64 v; // Save the status, each number accounts for 4 binary positions, can solve 16 digital problems struct EP_NODE_TAG * prev; // Parent Node Struct EP_NODE_TAG * SMALL, * BIG;} EP_NODE; EP_NODE M_AR [MAX_NODE]; EP_NODE * M_ROOT; long m_depth; // Search Depth EP_NODE M_OUT [MAX_DEP]; // Output Path // long move_gen (EP_NODE * NODE, EP_MOVE * MODE) {long pz; // 0 position UINT64 T = 0xf; for (PZ = NUM - 1; PZ> = 0; PZ -) {IF ((Node- > V & T) == 0) {Break; // Found 0 position} T << = 4; Switch (PZ) {Case 0: Move [0] .x = 0; Move [0]. Y = 1; Move [1] .x = 0; Move [1] .y = 3; Return 2; Case 1: Move [0] .x = 1; Move [0] .y = 0; Move [1] .x = 1; Move [1] .y = 2; Move [2] .x = 1; Move [2]. y = 4; ruln 3; case 2: move [0] .x = 2; Move [0] .y = 1; Move [1] .x = 2; Move [1] .y = 5; Return 2; Case 3: Move [0] .x = 3; Move [0] .y = 0; Move [1] .x = 3; Move [1] .y = 6; Move [2] .x = 3; Move [2 ] .y = 4; ruln 3; case 4: move [0] .x = 4; Move [0] .y = 1; Move [1] .x = 4; Move [1] .y = 3; Move [ 2] .x = 4; Move [2] .y = 5; Move [3] .x = 4; Move [3] .y = 7; Return 4; Case 5: Move [0] .x = 5; MOVE [0] .y = 2; Move [1] .x = 5; Move [1] .y = 4; Move [2] .x = 5; Move [2] .y = 8; Return 3; Case 6: Move [0] .x = 6; Move [0] .y = 3; Move [1] .x = 6; Move [1] .y = 7; Return 2; Case 7: Move [0] .x = 7; Move [0] .y = 6; Move [1] .x = 7; Move [1] .y = 4; Move [2]. x = 7; Move [2] .y = 8; Return 3; Case 8: Move [0] .x = 8; Move [0] .y = 5; Move [1] .x = 8; Move [1] .y = 7; Return 2;} return 0;} / * / long move (EP_NODE * N1, EP_MOVE * MV, EP_NODE * N2) // take a step, return the result after a step {char s [Num]; RTRANS (N1-> V, SS); XCHG (SS [MV-> x], SS [MV-> Y]); TRANS (SS, N2-> V); return 0;} / * * / long add_node (EP_NODE * NODE, LONG R) {EP_NODE * P = M_ROOT EP_NODE * Q; While (p) {q = p; if (p-> v == node-> v) return 0; Else IF (Node-> V> P-> V) P = P-> BIG; Else IF (Node-> V M_ar [r] .v = node-> v; m_ar [r] .prev = node-> prev; m_ar [r] .small = null; m_ar [r] .big = null; if (node-> v> q -> v) {q-> BIG = & m_ar [r];} else if (node-> v Return 1;} / * Get the depth of the node * / long get_node_depth (EP_NODE * NODE) {long d = 0; while (node-> prev) {d ; node = node-> prev;} Return D; / * Return value: success - return to the number of search nodes, the number of nodes is not enough - (- 1), no solution - (- 2) * / long bfs_search (char * begin, char * end) {long h = 0, r = 1 , C, I, J; EP_NODE L_END, NODE, * PNODE; EP_MOVE MV [4]; // Each situation is up to 4 ways of walking method TRANS (begin, m_ar [0] .v); trans (end, l_end.v ); M_ar [0] .prev = null; m_root = m_ar; m_root-> small = null; m_root-> big = null; while ((h IF (add_node, r)) R ; // can only search for new nodes in the historical node, otherwise the ring} will appear H ; Printf ("/ Rsearch ...% 9D /% D @% D", H, R, GET_NODE_DEPTH (& M_AR [h]));} IF (h == r) {RETURN-2;} else {return -1;}} / * * / long check_input (char * s, char a, long r) {long i; for (i = 0; i Return 1;} / * / long check_possible (char * begin, char * end) {char fs; long f1 = 0, F2 = 0; long i, j; for (i = 0; i F2 = fs;} IF ((F1 & 1) == (F2 & 1)) Return 1; Else Return 0;} / * / void output (void) {long i, j, k; char ss [num]; for (i = m_depth - 1; i> = 0; I -) {RTRANS (m_out [i] .v, SS); for (j = 0; j Printf ("/ n"); Printf ("/ n");}} / * / int main (void) {char S1 [NUM]; char S2 [NUM]; long r; char A; Printf ("INPUT BEGIN STATUS:"); r = 0; While (R Printf ("/ Ninput End Status:"); R = 0; While (R Printf ("/ n"); if (Check_Possible (S1, S2)) {r = bfs_search (S1, S2); Printf ("/ n"); if (r> = 0) {Printf ("Search Depth =% D, NODES =% ld / n ", m_depth, r); OUTPUT ();} else if (r == -1) {Printf (" not enouph nodes search./n ");} else if (r == -2) {Printf ("NO WAY to Do That./N");} else {printf ("unknown error./N");}}} else {printf ("mission impossible! / N");} Return 0 }