Description
In a 4*4 box placed a number of the same toys, someone want to rearrange these toys into his ideal state, the movement can only move the toys up and down in four directions, and move the position can not have toys, please use the minimum number of moves to move the initial toy state to the target state of someone's heart.
Input
The first 4 lines indicate the initial state of the toy, 4 digits per line 1 or 0, 1 means the toy is placed in the square, and 0 means no toys are placed. Then there is a blank line. The next 4 lines indicate the target state of the toy, 4 digits 1 or 0 per line, with the same meaning.
Output
An integer that requires a minimum number of moves.
Sample Input1111
0000
1110
0010
1010
0101
1010
0101Sample Output4
Alas a SB's search wrote I'm tired of love
#include <cstdio> #include <iostream> #define LL long longconst int mx[4]={1,0,-1,0};const int my[4]={0,1,0,- 1};const int v[4][4]={{1,2,4,8},{16,32,64,128},{256,512,1024,2048},{4096,8192,16384,32768}};struct Que{int a[4][4] , Step,rnk;} Q[100010],s,e;bool Mrk[100010];int t,w=1,nx,ny;inline ll read () {ll x=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline int getrnk (que a) {int tot=0,mul=1;for (int i=0;i<4;i++) for (int j=0;j<4;j++) {if (A.a[i][j]) tot+=mul;mul*=2 ;} return tot;} inline void ini (que &a) {for (Int. i=0;i<4;i++) for (int j=0;j<4;j++) {char Ch=getchar (), while (ch!= ' 0 ' && ch!= ' 1 ') Ch=getchar (); if (ch== ' 1 ') A.a[i][j]=1;}} int main () {INI (s); INI (e); Q[1]=s;q[1].rnk=getrnk (s); Mrk[q[1].rnk]=1;e.rnk=getrnk (e); if (e.rnk==q[1].rnk) {printf (" 0 "); return 0;} while (t<w) {que now=q[++t];now.step++;for (int. i=0;i<4;i++) for (int j=0;j<4;j++) IF (Now.a[i][j]) for (int k=0;k<4;k++) {nx=i+mx[k];ny=j+my[k];if (nx<0| | nx>3| | ny<0| | ny>3| | Now.a[nx][ny]) continue;now.rnk+=v[nx][ny]-v[i][j];if (Mrk[now.rnk]) {now.rnk-=v[nx][ny]-v[i][j];continue;} if (NOW.RNK==E.RNK) {printf ("%d\n", Now.step); return 0;} now.a[i][j]=0;now.a[nx][ny]=1;q[++w]=now;mrk[now.rnk]=1;now.a[i][j]=1;now.a[nx][ny]=0;now.rnk-=v[nx][ny]-v[i][ j];}} return 0;}
bzoj1054 [HAOI2008] Mobile Toys