1054: [HAOI2008] Mobile Toys time limit:10 Sec Memory limit:162 MB
submit:2246 solved:1246
[Submit] [Status] [Discuss] Description in a 4*4 box placed a number of the same toys, someone wants to put these toys back into his ideal state, the movement can only move the toy up and down in four directions, and move the position can not have toys, Please move the initial toy state to the target State in someone's mind with a minimum number of moves. The first 4 lines of Input indicate the initial state of the toy, 4 digits 1 or 0 per line, 1 means that the toy is placed in the square, and 0 indicates that 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 Output4HINT Source
Question analysis: Perhaps too busy, I unexpectedly make bit arithmetic write this problem (a moment brain pumping ) ...
Simple BFS, positive solution plus a hash to fix
Think I've saved a hash like this, Qaq.
Code (Big God do not spray)
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <stack > #include <vector> #include <algorithm>//#include <cmath>using namespace Std;const int INF = 9999999; #define LL long longinline int read () {int X=0,f=1;char c=getchar (); IsDigit (c); C=getchar ()) if (c== '-') f=-1;for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} int N,e;bool Vis[65539];char c;int l=1,r=1;struct data{int st,k;} Que[700001];void BFS () {que[l].st=0,que[l].k=n;int step,now;if (que[l].k==e) {printf ("%d\n", que[l].st); return;} while (l<=r) {now=que[l].k,step=que[l].st;for (int. i=1;i<=4;i++) {for (int j=1;j<=4;j++) {if ((Now>> (4 -i) *4+4-j)) {&1)) {for (int p=0;p<4;p++) {if (p==0&&i!=1&&! ( (Now>> (i-1) *4+4-j) &1) &&!vis[now-(1<< (4-i) *4+4-j) + (1<< ((4-i) *4+8-j)]) { que[++r].k=now-(1<< ((4-i) *4+4-j) + (1<< ((4-i) *4+8-j)), if (que[r].k==e) {printf ("%d\n", step+1); return;} que[r].st=step+1;vis[que[R].k]=1;} if (p==1&&i!=4&&! ( (Now>> (i+1) *4+4-j) &1) &&!vis[now-(1<< (4-i) *4+4-j) + (1<< ((4-i) *4-j))] {Que[ ++r].k=now-(1<< ((4-i) *4+4-j) + (1<< ((4-i) *4-j)), if (que[r].k==e) {printf ("%d\n", step+1); return;} Que[r].st=step+1;vis[que[r].k]=1;} if (p==2&&j!=1&&! ( (Now>> ((4-i) *4+4-(j-1)) &1) &&!vis[(1<< ((4-i) *4+4-j) + (1<< ((4-i) *4+4-(j-1))]) { que[++r].k=now-(1<< ((4-i) *4+4-j) + (1<< (4-i) *4+4-(j-1)), if (que[r].k==e) {printf ("%d\n", step+1); return;} Que[r].st=step+1;vis[que[r].k]=1;} if (p==3&&j!=4&&! ( (Now>> ((4-i) *4+4-(j+1)) &1) &&!vis[now-(1<< ((4-i) *4+4-j)) + (1<< ((4-i) *4+4-(j+1))] {que[++r].k=now-(1<< ((4-i) *4+4-j) + (1<< ((4-i) *4+4-(j+1))), if (que[r].k==e) {printf ("%d\n", step+1); return;} que[r].st=step+1;vis[que[r].k]=1;}}}} l++;}} int main () {//freopen (".", "R", stdin),//freopen (". Out", "w", stdout); int tmp=16;for (int i=1;i<=4;i++) for (inT j=1;j<=4;j++) {cin>>c; n=n+ (1<< (tmp-1)) * (c ' 0 '); tmp--;} vis[n]=true;tmp=16;for (int i=1;i<=4;i++) for (int j=1;j<=4;j++) {cin>>c; E=e+ (1<< (tmp-1)) * (c ' 0 '); tmp--;} BFS (); return 0;}
"Bit operation" "BFS" mobile toys