玩具城移动小盒,1054: [HAOI2008]移动玩具

1054: [HAOI2008]移动玩具

Time Limit: 10 Sec Memory Limit: 162 MB Submit: 176 Solved: 100 [Submit][Status][Discuss]

Description

在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初始的玩具状态移动到某人心中的目标状态。

Input

前4行表示玩具的初始状态,每行4个数字1或0,1表示方格中放置了玩具,0表示没有放置玩具。 接着是一个空行。 接下来4行表示玩具的目标状态,每行4个数字1或0,意义同上。

Output

一个整数,所需要的最少移动次数。

Sample Input

1111 0000 1110 0010 1010 0101 1010 0101

Sample Output

4

HINT


Source

NOIP普及组难度的题。。。练练双搜
#include #include #include #include #include #define cp freopen #define rzy inline #define rl scanf("\n") #define rep(i,a,b) for (int i=a;i<=b;i++) using namespace std; const int dx[4]={1,-1,0,0}; const int dy[4]={0,0,1,-1}; int v[100000],f[100000],b[100000]; struct node { char map[4][4]; int s; node():s(0){} rzy void Read() {rep(i,0,3) {rep(j,0,3) cin>>map[i][j];rl;}} rzy int state() { int ans=0; rep(i,0,3) rep(j,0,3) { ans=ans<<1; ans=ans+map[i][j]-'0'; } return ans; } rzy bool expand(node w,int x,int y,int i) { if (w.map[x][y]!='1') return false; int tx=x+dx[i],ty=y+dy[i]; if (tx>3||ty>3||tx<0||ty<0) return false; if (w.map[tx][ty]!='0') return false; w.map[tx][ty]='1',w.map[x][y]='0'; w.s++;*this=w; return true; } rzy bool operator ==(node w) { rep(i,0,3) rep(j,0,3) if (w.map[i][j]!=map[i][j]) return false; return true; } }start,end; queue fs,bs; int main() { node start,end,now; start.Read();rl; end.Read(); fs.push(start);bs.push(end); if (start==end) {cout<<"0"<bs.size()) { node H=bs.front();bs.pop(); rep(x,0,3) rep(y,0,3) rep(i,0,3) { int tmp; if (!now.expand(H,x,y,i)) continue; if (v[(tmp=now.state())]==1) { cout<
Tags: 

延伸阅读

最新评论

发表评论