1054: [HAOI2008] Mobile Toys time limit:10 Sec Memory limit:162 MB
[Submit] [Status] [Discuss] 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 Input1111
0000
1110
0010
1010
0101
1010
0101Sample Output4
#include <bits/stdc++.h>using namespacestd;#definell Long Long#definePi (4*atan (1.0))#defineEPS 1e-14Const intn=2e5+Ten, m=1e6+Ten, inf=1e9+Ten, mod=1e9+7;Constll inf=1e18+Ten;struct is{ Chara[5][5]; intstep;};Charmp[5][5];Charans[5][5];map<string,int>m;stringCheckCharmp[][5]){ stringA=""; for(intI=0;i<4; i++) for(intt=0;t<4; t++) A+=Mp[i][t]; returnA;} Queue< is>Q;intffintXinty) { if(x<0|| x>=4|| y<0|| y>=4) return 0; return 1;}intxx[4]={0,1,0,-1};intyy[4]={1,0,-1,0};intMain () { for(intI=0;i<4; i++) scanf ("%s", Mp[i]); for(intI=0;i<4; i++) scanf ("%s", Ans[i]); M[check (MP)]=1; isF; for(intI=0;i<4; i++) for(intt=0;t<4; t++) F.a[i][t]=Mp[i][t]; F.step=0; Q.push (f); int out; while(!Q.empty ()) { isb=Q.front (); Q.pop (); intflag=1; for(intI=0;i<4; i++) { for(intt=0;t<4; t++) if(b.a[i][t]!=Ans[i][t]) {Flag=0; Break; } } if(flag) { out=B.step; Break; } for(intI=0;i<4; i++) { for(intt=0;t<4; t++) if(b.a[i][t]=='1') { for(intj=0;j<4; j + +) { intxxx=i+Xx[j]; intyyy=t+Yy[j]; //cout<<xxx<< "<<yyy<<" "<<ff (xxx,yyy) <<endl; if(FF (XXX,YYY) &&b.a[xxx][yyy]=='0') {b.a[xxx][yyy]='1'; B.a[i][t]='0'; B.step++; if(M[check (B.A)]==0) Q.push (b), M[check (B.A)]=1; B.step--; B.A[XXX][YYY]='0'; B.a[i][t]='1'; } }}}} printf ("%d\n", out); return 0;}
Bzoj 1054: [HAOI2008] Mobile toy BFS