Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1195
Search on three kinds of rules, one is + 11 is-1 There is an exchange, with BFS search on the line, referring to the idea of the great God ...
Code:
#include <cstdio> #include <queue> #include <cstring>using namespace Std;int a[5];int b[5];struct Node {int di[5]; int St;}; int v[10][10][10][10];void BFs () {queue<node> q; Node F; for (int i = 0; i < 4; ++i) f.di[i] = A[i]; F.st = 0; Q.push (f); while (!q.empty ()) {Node t = Q.front (); Q.pop (); int flag = 1; for (int i = 0; i < 4; ++i) {if (T.di[i]! = B[i]) {flag = 0; Break }} if (flag) {printf ("%d\n", t.st); return; } Node p; for (int i = 0; i < 4; ++i)//+1 {p = t; if (p.di[i] = = 9) P.di[i] = 1; else p.di[i]++; P.st = t.st + 1; if (!v[p.di[0]][p.di[1]][p.di[2]][p.di[3]]) {v[p.di[0]][p.di[1]][p.di[2]][p.di[3]] = 1; Q.push (P); } } for (int i = 0; i < 4; ++i)//-1 {p = t; if (p.di[i] = = 1) p.di[i] = 9; else p.di[i]--; P.st = t.st + 1; if (!v[p.di[0]][p.di[1]][p.di[2]][p.di[3]]) {v[p.di[0]][p.di[1]][p.di[2]][p.di[3]] = 1; Q.push (P); }} for (int i = 0; i < 3; ++i)//interchange {p = t; P.di[i] = t.di[i + 1]; P.di[i + 1] = T.di[i]; P.st = t.st + 1; if (!v[p.di[0]][p.di[1]][p.di[2]][p.di[3]]) {v[p.di[0]][p.di[1]][p.di[2]][p.di[3]] = 1; Q.push (P); }}}}int Main () {int _; scanf ("%d", &_); while (_--) {memset (v,0,sizeof (v)); Char s[10],s1[10]; scanf ("%s", s); for (int i = 0;i < 4;++i) A[i] = s[i]-' 0 '; scanf ("%s", S1); for (int i = 0; i < 4; ++i) B[i] = S1[i]-' 0 '; BFS (); }}
Hdu1195open the Lock