State compression + reverse BFS.
An array of directions is any adjacent two points (minus 1 at initialization), and each vertex has 4 adjacent points. Therefore, there is a 16*4/2=32 direction. can be found by order.
1 /*3220*/2#include <iostream>3#include <queue>4#include <algorithm>5#include <cstdio>6#include <cstring>7 using namespacestd;8 9 Ten Charvisit[1<< -]; One Const intEnd =0x0ff; A intBeg; - intdir[ +][2] = { - 1,2,1,3,1,5,1,9, the 2,4,2,6,2,Ten, - 3,4,3,7,3, One, - 4,8,4, A, - 5,6,5,7,5, -, + 6,8,6, -, - 7,8,7, the, + 8, -, A 9,Ten,9, One,9, -, at Ten, A,Ten, -, - One, A, One, the, - A, -, - -, -, -, the, - -, -, - the, - in }; - to voidBFs () { + intI, J, K, TMP; -queue<int>Q; the intA, B, s; * CharT; $ Panax Notoginsengmemset (Visit,-1,sizeof(visit)); -Visit[end] =0; the Q.push (end); + A while(!Q.empty ()) { thes =Q.front (); + Q.pop (); -t =Visit[s]; $ if(T >=3) $ Continue; -++T; - for(i=0; i< +; ++i) { theA = S & (1<<dir[i][0]); -B = S & (1<<dir[i][1]);Wuyi if(A ^b) { theTMP = (s ^ (1<<dir[i][0])) ^ (1<<dir[i][1]); - if(Visit[tmp] <0) { WuVISIT[TMP] =T; - Q.push (TMP); About } $ } - } - } - } A + voidinit () { the inti; - $ for(i=0; i< +; ++i) { the--dir[i][0]; the--dir[i][1]; the } the - BFS (); in } the the intMain () { About intT, TT; the intI, J, K; the the #ifndef Online_judge +Freopen ("data.in","R", stdin); -Freopen ("Data.out","W", stdout); the #endifBayi the init (); thescanf"%d", &TT); - for(t=1; t<=tt; ++t) { -Beg =0; the for(i= the; i>=0; --i) { thescanf"%d", &j); the if(j) theBeg |= (1<<i); - } theK =Visit[beg]; the if(K <0) theprintf"Case #%d:more\n", T);94 Else theprintf"Case #%d:%d\n", T, K); the } the 98 return 0; About}
"Hdoj" 3220 Alice ' s Cube