Put a wave on the surface first
1054: [HAOI2008] Mobile Toys time limit:10 Sec Memory limit:162 MB
submit:2288 solved:1270 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
An integer that requires a minimum number of moves.
Sample Input 1111
0000
1110
0010
1010
0101
1010
0101Sample Output 4
This problem at first glance feels strange, then a look at the data size fixed to 16-bit, the pressure DP can be overThe strategy I chose is BFS, which enumerates the possible transition states from the start State, and updates the state into the teamThe bag Horse is as follows:GitHub
1#include <queue>2#include <cstdio>3#include <cctype>4#include <cstring>5#include <cstdlib>6#include <iostream>7#include <algorithm>8 9 intdp[1<< -];Ten intorigin; One intgoal; A BOOLinqueue[1<< -]; - - voidInitialize (); the voidBFS (int); - - intMain () { - Initialize (); + BFS (origin); -printf"%d\n", Dp[goal]); + return 0; A } at - voidBFS (intorigin) { - intTop,target; -std::queue<int>Q; - Q.push (origin); -dp[origin]=0; in while(!Q.empty ()) { -top=Q.front (); to Q.pop (); + for(intI=0;i< -; i++){ - if((top& (1<<i)) = =0) the Continue; * if(i>=4&& (top& (1<< (I-4)))==0){ $Target= (top^ (1<<i) ^ (1<< (I-4));Panax Notoginseng if(dp[target]>dp[top]+1){ -dp[target]=dp[top]+1; the if(!Inqueue[target]) { + Q.push (target); Ainqueue[target]=true; the } + } - } $ if(i<= One&& (top& (1<< (i+4)))==0){ $Target= (top^ (1<<i) ^ (1<< (i+4)); - if(dp[target]>dp[top]+1){ -dp[target]=dp[top]+1; the if(!Inqueue[target]) { - Q.push (target);Wuyiinqueue[target]=true; the } - } Wu } - if(i%4>0&& (top& (1<< (I-1)))==0){ AboutTarget= (top^ (1<<i) ^ (1<< (I-1)); $ if(dp[target]>dp[top]+1){ -dp[target]=dp[top]+1; - if(!Inqueue[target]) { - Q.push (target); Ainqueue[target]=true; + } the } - } $ if(i%4<3&& (top& (1<< (i+1)))==0){ theTarget= (top^ (1<<i) ^ (1<< (i+1)); the if(dp[target]>dp[top]+1){ thedp[target]=dp[top]+1; the if(!Inqueue[target]) { - Q.push (target); ininqueue[target]=true; the } the } About } the } the } the } + - voidInitialize () { theFreopen ("movea.in","R", stdin);BayiFreopen ("Movea.out","W", stdout); the intpos= the; the CharCh=GetChar (); - while(pos>=0){ - while(!isdigit (CH)) theCh=GetChar (); theOrigin^= (ch-'0') <<Pos; the--Pos; theCh=GetChar (); - } thepos= the; the while(pos>=0){ the while(!isdigit (CH))94Ch=GetChar (); theGoal^= (ch-'0') <<Pos; the--Pos; theCh=GetChar ();98 } AboutMemset (DP,0x7F,sizeof(DP)); - //printf ("%x%x\n", origin,goal);101}
Backup
And then the time of the picture package
[Haoi 2005] [Bzoj 1054] Mobile Toys