Bzoj1054: [HAOI2008] Mobile Toys

Source: Internet
Author: User
<span id="Label3"></p><p><p>hash+bfs; pay attention to special Circumstances. (seems to even Sort.) Lower_bound is not the data of the small direct sentence weight ...</p></p><pre class="brush:csharp;collapse:true;;gutter:true;">#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std; #define REP (i,n) for (int. I=1;i<=n;i++) #define CLR (x,c) memset (x,c,sizeof (x)) #define REP (i,s,t) for (int I=s;i <=t;i++) struct Node{int a[5][5];int dist;}; Node A,b,q[65540];char tmp[5];int xx[5]={0,0,0,1,-1};int yy[5]={0,1,-1,0,0};bool vis[65540];int get (node x) {int k=1, Ans=0;rep (i,4) Rep (j,4) {if (x.a[i][j]) ans+=k;k<<=1;} Return ans;} void init () {rep (i,4) {scanf ("%s", tmp); rep (j,4) a.a[i][j]=tmp[j-1]-' 0 ';} Rep (i,4) {scanf ("%s", tmp); rep (j,4) b.a[i][j]=tmp[j-1]-' 0 '; b.dist=0;}} void work () {clr (vis,false); int s=get (A), t=get (B); if (s==t) {printf ("0\n"); return;} int l=1,r=1;q[1]=b;while (l<=r) {node x=q[l];rep (i,4) Rep (j,4) if (x.a[i][j]) rep (k,4) {int Tx=i+xx[k],ty=j+yy[k];if ( !tx| |! ty| | tx>4| | ty>4| | X.a[tx][ty]) continue;swap (x.a[i][j],x.a[tx][ty]), x.dist++;int temp=get (x), if (!vis[temp]) {if (temp==s) {printf ("%d \ n ", x.dist); return;} vis[temp]=1;q[++r]=x;} Swap (x.a[i][j],x.a[tx][ty]); x.dist--;} l++;}} int main () {init (); work (); return 0;}</pre><p><p>  </p></p> <center> <center> 1054: [HAOI2008] Mobile Toys time <span class="green">limit:10 Sec <span class="green">Memory limit:162 MB<br><span class="green">submit:1764 <span class="green">solved:972<br>[Submit] [Status] [discuss]</span></span></span></span> </center> </center>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<p><p>An integer that requires a minimum number of Moves.</p></p>Sample Input<span class="sampledata"><span class="sampledata">1111<br>0000<br>1110<br>0010<br><br>1010<br>0101<br>1010<br>0101</span></span>Sample Output<span class="sampledata"><span class="sampledata">4</span></span>HINT Source <center> <center> [Submit] [Status] [discuss] </center> </center><p><p>Bzoj1054: [HAOI2008] Mobile Toys</p></p></span>

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.