P4289 [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/output format Input Format:

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 Format:

An integer that requires a minimum number of moves.

input/Output sample Input Sample # #:
1111000011100010 1010010110100101
Sample # # of output:
4

Solution:

The subject compares the water , but I adjust quickly the mentality burst. (Because of the variable name of the problem, the wrong number of variables, make it always thought that the hand of the queue is wrong, alas ~ too dish ~!) )

From a certain state to another State, and given the transfer method, it is not difficult to think, general direct wide search on the $ok$.

The key is how to weigh and record the state, at this time because each state is a $16$ bit $0/1$ string, it is natural to think of state compression: with $1$ to $2^{16}=65536$ to record each state, burst search $ac$. As for how to transfer the direct simulation well! (I am more stupid, the method is more cumbersome, say the hand pushed a wave of transfer process, ~ funny ~)

Code:

1#include <bits/stdc++.h>2 #definefor (I,A,B) for (int (i) = (a);(i) <= (b);(i) + +)3 using namespacestd;4 Const intn=65560;5 intst,ed,op[ -],opt,mp[n],ans;6 intdx[ -][4]={7     0,0,0,0, 8     1,4,0,0, 9-1,1,4,0, Ten-1,1,4,0,  One-1,4,0,0,  A-4,4,1,0,  --1,1,4,-4,  --1,1,4,-4, the-4,4,-1,0, --4,4,1,0, --1,1,4,-4, --1,1,4,-4, +-4,4,-1,0, --4,1,0,0, +-4,-1,1,0, A-4,-1,1,0, at-4,-1,0,0 - }; -queue<int>Q; - Chars; - BOOLVis[n]; - intMain () { inIos::sync_with_stdio (0); -for (I,1, -) cin>>s,st= (st<<1) +s-'0'; tofor (I,1, -) cin>>s,ed= (ed<<1) +s-'0'; +Q.push (ST); vis[st]=1; -      while(!Q.empty ()) { the         intU=q.front (); Q.pop ();intp=u; *         if(u==ed) Break; $for (I,1, -) {op[ --i]=p%2, p>>=1;}Panax Notoginsengfor (I,1, -) for (J,0,3) -         if(dx[i][j]!=0){ the             if(op[i+dx[i][j]]!=Op[i]) { +Swap (op[i+dx[i][j]],op[i]); Aopt=0; theFor (K,1, -) opt= (opt<<1)+Op[k]; +                 if(!vis[opt]) { -vis[opt]=1, Q.push (opt), mp[opt]=mp[u]+1; $                 } $Swap (op[i+dx[i][j]],op[i]); -             } -         } the     } -cout<<mp[ed];Wuyi     return 0; the}

P4289 [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.