Title 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 input
1111
0000
1110
0010
1010
0101
1010
0101
Sample output
4
Exercises
BFs
Press the state into a binary number (you should also try a 16-D array), then BFS.
#include <cstdio> #include <cstring> #include <queue>using namespace Std;int q[70000], L, R, F[70000];c Har Str[10];int read () {int i, j, num = 0, B = 1;for (i = 0; i < 4; i + +) {scanf ("%s", str); for (j = 0; j < 4; j + +) num + = (str[j]-' 0 ') * b, B <<= 1;} return num;} int main () {int A, B, U, V, i;a = Read (), B = Read (), memset (F,-1, sizeof (f)), q[0] = a;f[a] = 0;while (l <= r) {u = Q[l + +];if (U = = b) {printf ("%d\n", F[u]); return 0;} for (i = 1; I <=; i + +) {if (U & (1 << (i-1))) {v = u ^ (1 << (i-1)); if (i > 4 && (~V) & (1 << (i-5)) && f[v ^ (1 << (i-5))] = = 1) f[v ^ (1 << (i-5))] = F[u] + 1, q[++r] = v ^ (1 << (i-5)), if (I < 3 && (~V) & (1 << (i + 3)) && f[v ^ (1 << (i + +))] = = -1) f[v ^ (1 << (i + 3))] = F[u] + 1, q[++r] = v ^ (1 << (i + 3)), if ((i-1)% 4 && (~V) & (1 < ;< (i-2)) && f[v ^(1 << (i-2))] = =-1) f[v ^ (1 << (i-2))] = F[u] + 1, q[++r] = v ^ (1 << (i-2)); if (i% 4 && (~V) & (1 << ; i) && f[v ^ (1 << i)] = = 1) f[v ^ (1 << i)] = F[u] + 1, q[++r] = v ^ (1 << i);}} return 0;}
"bzoj1054" [HAOI2008] Mobile Toys