Title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=1054
Exercises
Blind Basic Search is no hope, or normal BFS more reliable
See the 4*4 of the lattice of each row together is a 01 string, can be used similar to the state compression of the idea to solve, convert it into a decimal number, mainly to determine whether it has reached the ideal state
Queue storage State, in the BFS when the team first, for each piece of the four kinds of movement to create a new state, and then queue processing can be
(A little confused about why the "hash" was used to name this state compression ...) )
1#include <cstdio>2#include <cstring>3 intst,en,_x[]={0,0,0,-1,1},_y[]={0,1,-1,0,0};4 BOOLvis[65536];5 structnode6 {7 BOOLstatus[5][5];8 intDEP;9}q[65536];TenInlineintHashBOOLa[5][5]) One { A intk=1, ans=0; - for(intI=1; i<=4; i++) - { the for(intj=1; j<=4; j + +) - { -ans+=a[i][j]*K; -k<<=1; + } - } + returnans; A } at voidInit () - { - Chartemp[5]; - BOOLtemp2[5][5]; - for(intI=1; i<=4; i++) - { inscanf"%s", temp+1); - for(intj=1; j<=4; j + +) to { +q[1].status[i][j]=temp[j]-'0'; - } the } * for(intI=1; i<=4; i++) $ {Panax Notoginsengscanf"%s", temp+1); - for(intj=1; j<=4; j + +) the { +temp2[i][j]=temp[j]-'0'; A } the } +St=hash (q[1].status); -en=hash (TEMP2); $ } $ voidBFS () - { -vis[st]=true; the intHead=1, tail=2; - while(head<tail)Wuyi { the for(intI=1; i<=4; i++) - { Wu for(intj=1; j<=4; j + +) - { About if(Q[head].status[i][j]) $ { - for(intk=1; k<=4; k++) - { - intx=i+_x[k],y=j+_y[k]; A if(q[head].status[x][y]| |! X| |! y| | X>4|| Y>4)Continue; +q[head].status[i][j]=false; theq[head].status[x][y]=true; - inthash_of_status=hash (q[head].status); $ if(!Vis[hash_of_status]) the { the if(hash_of_status==en) the { theprintf"%d", q[head].dep+1); - return; in } thememcpy (Q[tail].status,q[head].status,sizeof(Q[tail].status)); theq[tail++].dep=q[head].dep+1; Aboutvis[hash_of_status]=true; the } theq[head].status[i][j]=true; theq[head].status[x][y]=false; + } - } the }Bayi } thehead++; the } - } - intMain () the { the init (); the if(st==en) the { -printf"0\n"); the return 0; the } the BFS ();94 return 0; the}
Bzoj 1054 Mobile Toys