A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small
squares of equal size. A unique letter of the alphabet was printed on each small square. Since there
were only 24 squares within the frame, the frame also contained an empty position which was the same
size as a small square. A square could be moved into that empty position if it were immediately to the
right, to the left, above, or below the empty position. The object of the puzzle was to slide squares
into the empty position so that the frame displayed the letters in alphabetical order.
The illustration below represents a puzzle in its original configuration and in its configuration after
the following sequence of 6 moves:
题目很长很长,而且输入输出就巨扯淡,但是题目大意很简单,就是按着方向去移动木块,跟我们小时候玩的积木一样.直接上代码,
#include <iostream>
#include <cstdio>
#include<cstring>
using namespace std;
int main()
{
char a[5][7];
char cmd[1005];
int kase=0;
while(gets(a[0]))//也是参考了一位大牛的方法这么写的
{
if(a[0][0]=='Z') break;
for(int i=1; i<5; i++)
gets(a[i]);
int xx=0,yy=0;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
if(a[i][j]==' ')
{
xx=i,yy=j;
break;
}
}
int k=0;
while(cin>>cmd[k])
{
if(cmd[k]=='0') break;
else k++;
}
cmd[k]=0;
getchar();
int flag=0,x=xx,y=yy;
for(int i=0; cmd[i]; i++)
{
if(cmd[i]=='A')
x=xx-1,y=yy;
else if(cmd[i]=='B')
x=xx+1,y=yy;
else if(cmd[i]=='L')
x=xx,y=yy-1;
else if(cmd[i]=='R')
x=xx,y=yy+1;
if(x<0||x>4||y<0||y>4)
{
flag=1;
break;
}
else
{
a[xx][yy]=a[x][y];
a[x][y]=' ';
xx=x,yy=y;
}
}
if(kase++)
cout<<endl;
cout<<"Puzzle #"<<kase<<":"<<endl;
if(flag)
cout<<"This puzzle has no final configuration."<<endl;
else
{
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
if(j) cout<<" ";
cout<<a[i][j];
}
cout<<endl;
}
}
}
return 0;
}
努力加油a啊,(o)/~