1054: [HAOI2008] Mobile Toys time limit:10 Sec Memory limit:162 MB
submit:1388 solved:764
[Submit] [Status] [Discuss] 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 Input1111
0000
1110
0010
1010
0101
1010
0101Sample Output4HINT Source
The following: BFS explosion search
Nest first Vis forgot to maintain the!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
The MLE of n-hair ah ah ah ..... ..... ....... ....................
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <stack>6#include <queue>7#include <cstring>8 #definePAU Putchar (")9 #defineENT Putchar (' \ n ')Ten using namespacestd; One Const intlim=1<< -; A Const intdx[]={0,0,-1,1}; - Const intdy[]={-1,1,0,0}; - BOOLVis[lim]; the intIdintIintj) {returni*4+J;} - structdata{intkey,d;}; - intPackBOOLa[4][4]){ - intres=0; + for(intI=0;i<4; i++) - for(intj=0;j<4; j + +) + if(A[i][j]) res|= (1<<id (i,j));returnRes; A } atInlineintRead () { - intx=0;BOOLsig=1;CharCh=GetChar (); - for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') sig=0; - for(; isdigit (ch); Ch=getchar ()) x=Ten*x+ch-'0'; - returnsig?x:-x; - } inInlinevoidWriteintx) { - if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; to intlen=0, buf[ -]; while(x) buf[len++]=x%Ten, x/=Ten; + for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; - } the BOOLstart[4][4],endin[4][4];ints,t; * Chars[6]; $ intBFs () {Panax Notoginsengqueue<data>q; Q.push (data) {S,0}); vis[s]=true; - while(!Q.empty ()) { theData x=Q.front (); Q.pop (); + if(x.key==t)returnx.d; A for(intI=0;i<4; i++){ the for(intj=0;j<4; j + +){ + if(x.key& (1<<id (I,J))) for(intD=0;d <4;d + +){ - inti2=i+dx[d],j2=j+Dy[d]; $ if(i2>=0&&i2<4&&j2>=0&&j2<4&& (!) ( x.key& (1<<ID (I2,J2)))) { $ intV=x.key;v|= (1<<id (I2,J2)); v-= (1<<ID (i,j)); - if(!vis[v]) Q.push (data) {v,x.d+1}), vis[v]=true; - } the } - }Wuyi } the}return-1; - } Wu intMain () { - for(intI=0;i<4; i++){ Aboutscanf"%s", s); $ for(intj=0;j<4; j + +)if(s[j]-'0') start[i][j]=true; - } - for(intI=0;i<4; i++){ -scanf"%s", s); A for(intj=0;j<4; j + +)if(s[j]-'0') endin[i][j]=true; + } theS=pack (start); t=Pack (Endin); - Write (BFS ()); $ return 0; the}
Bzoj 1054 [HAOI2008] Mobile Toys