bzoj1054| HAOI2008 Mobile Toys-Guangzhou Search

Source: Internet
Author: User

Description
in a 4*4 box placed a number of the same toys, someone wants to rearrange the toys to 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 move the initial toy state to the target State in someone's mind with a minimum number of moves.
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

Analysis: Wide search + hash optimization

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespacestd;Const intdx[5]={0,-1,0,1,0};Const intdy[5]={0,0,1,0,-1};intans[5][5];intuse[66666];structnode{inta[5][5],step;} q[80001];intHashinta[5][5]){    intk=1, s=0;  for(intI=1; i<=4; i++)         for(intj=1; j<=4; J + +) {s+=k*A[i][j]; K=k<<1; }    returns;}intMain () {memset (use,0,sizeof(use));  for(intI=1; i<=4; i++)    {         for(intj=1; j<=4; J + +)        {            CharC; C=GetChar (); q[1].a[i][j]=c-'0';    } getchar ();    } getchar ();  for(intI=1; i<=4; i++)    {         for(intj=1; j<=4; J + +)        {            CharC; C=GetChar (); ANS[I][J]=c-'0';    } getchar (); }    intSt=hash (q[1].a); inten=hash (ans); if(st==en) {printf ("0");return 0; } USE[ST]=1; intH=0, t=1; q[1].step=0;  while(h<=t) {h++;  for(intI=1; i<=4; i++)               for(intj=1; j<=4; J + +)                  if(Q[h].a[i][j]) for(intk=1; k<=4; k++)                      {                         intx=i+Dx[k]; inty=j+Dy[k]; if(x>4|| Y>4|| x<1|| y<1|| Q[t].a[x][y])Continue;                         Swap (q[h].a[i][j],q[h].a[x][y]); intHh=hash (Q[H].A); if(!USE[HH]) {                                    if(hh==en) {printf ("%d", q[h].step+1); System"Pause");return 0; } T++; memcpy (Q[T].A,Q[H].A,sizeof(Q[T].A)); Q[t].step=q[h].step+1; USE[HH]=1;                    } swap (Q[h].a[i][j],q[h].a[x][y]); }    }    return 0;}

bzoj1054| HAOI2008 Mobile Toys-Guangzhou Search

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.