"bzoj1054" [HAOI2008] Mobile Toys

Source: Internet
Author: User

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 &lt ;< (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 <&lt ; i) && f[v ^ (1 << i)] = = 1) f[v ^ (1 << i)] = F[u] + 1, q[++r] = v ^ (1 << i);}} return 0;}

"bzoj1054" [HAOI2008] Mobile Toys

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.