"Title description":
Magic Board Magic Squares
"Ideas":
Does it feel like eight-digit?
The obvious wide search, the state of the magic Plate as an arrangement, then the state is at most \ (8! = 40320\) species, space is acceptable, for the first few permutations can be implemented with comtop (I want to do eight digital when you are aware of this routine), Then according to the topic of three ways to transfer the state, each state transferred out \ (3\) sub-state, pay attention to the weight! , once the target State appears, the number of layers searched must be the minimum number of steps to get that state. Finally, the code is much more detailed, so be careful.
#include <cstdio> #include <queue> #include <algorithm>using namespace Std;int calc[]={ 1,1,2,6,24,120,720,5040};int mp[3][5];bool vis[45000];int rest[10];int a[45000][10];int ans;int ans;int step[45000]; int St;char Last[45000];char Endd[45000];int fa[45000];inline bool Judge () {//Comtop expand tag ans = 0;int id = 7; for (int i=1;i<=8;++i) rest[i] = i-1; for (int i=1;i<=4;++i) {ans + = (Rest[mp[1][i]]) * Calc[id]; for (int j=mp[1][i]+1;j<=8;++j) rest[j]--; id--; } for (int i=1;i<4;++i) {ans + = (Rest[mp[2][i]]) * Calc[id]; for (int j=mp[2][i]+1;j<=8;++j) rest[j]--; id--; } ans++; if (Vis[ans]) {if (ans = = ans) return 1; else return 0; } Vis[ans] = 1; id = 0; for (int i=1;i<=4;++i) A[ans][++id] = Mp[1][i]; for (int i=1;i<=4;++i) A[ans][++id] = Mp[2][i]; return 1;} inline bool A (int x) {int id = 0; for (int i=1;i<=4;++i) mp[1][i] = A[x][++id]; for (int i=1;i<=4;++i) mP[2][i] = A[x][++id]; Swap (mp[1], mp[2]); return judge ();} inline bool B (int x) {int id = 0; for (int i=1;i<=4;++i) mp[1][i] = A[x][++id]; for (int i=1;i<=4;++i) mp[2][i] = A[x][++id]; Swap (Mp[1][4], mp[1][1]); Swap (Mp[2][4], mp[2][1]); for (int i=4;i>=3;--i) {swap (Mp[1][i], mp[1][i-1]); Swap (Mp[2][i], mp[2][i-1]); } return judge ();} inline bool C (int x) {int id = 0; for (int i=1;i<=4;++i) mp[1][i] = A[x][++id]; for (int i=1;i<=4;++i) mp[2][i] = A[x][++id]; Swap (mp[1][2], mp[1][3]); Swap (mp[1][2], mp[2][2]); Swap (mp[2][2], mp[2][3]); return judge ();} queue<int>q;inline void BFs () {Q.push (ST); while (!q.empty ()) {int u = q.front (); Q.pop (); if (A (U)) {Q.push (ans), step[ans] = Step[u] + 1; Last[ans] = ' A '; Fa[ans] = u; if (ans = = ans) {printf ("%d\n", Step[ans]); return;} } if (B (U)) {Q.push (ans), step[ans] = Step[u] + 1; Last[ans] = ' B '; Fa[ans] = u; if (ans = = ans) {printf ("%d\n", Step[ans]); return;} } if (C (U)) {Q.push (ans), step[ans] = Step[u] + 1; Last[ans] = ' C '; Fa[ans] = u; if (ans = = ans) {printf ("%d\n", Step[ans]); return;} }}}int Main () {for (int i=1;i<=4;++i) mp[1][i] = i; for (int i=1;i<=4;++i) mp[2][i] = 9-i; Judge (); st = Ans;last[st] = ' & '; for (int i=1;i<=4;++i) a[ans][i] = i; for (int i=5;i<=8;++i) a[ans][i] = 13-i; for (int i=1;i<=4;++i) scanf ("%d", &mp[1][i]); for (int i=1;i<=4;++i) scanf ("%d", &mp[2][5-i]); Judge (); ans = ans; if (st = = Ans) {puts ("0"); return 0; } BFS (); int x = Ans;int num = 0; while (last[x]! = ' & ') {Endd[++num] = last[x]; x = Fa[x]; } for (int i=num;i>=1;--i) printf ("%c", Endd[i]); return 0;}
Magic Board Magic squares