A children's puzzle that waspopular 30 years ago consisted of a 5x5 frame which contained 24 small squaresof equal size. A unique letter of the alphabet was printed on each smallsquare. Since there were only 24 squares within the frame, the frame also containedan empty position which was the same size as a small square. A square could bemoved into that empty position if it were immediately to the right, to theleft, above, or below the empty position. The object of the puzzle was to slidesquares into the empty position so that the frame displayed the letters inalphabetical order.
The illustration below representsa puzzle in its original configuration and in its configuration after thefollowing sequence of 6 moves:
1) The square above the empty position moves.
2) The square to the right of the empty positionmoves.
3) The square to the right of the empty positionmoves.
4) The square below the empty position moves.
5) The square below the empty position moves.
6) The square to the left of the empty positionmoves.
Write a program to display resulting frames given theirinitial configurations and sequences of moves.
Input for your program consists of several puzzles. Eachis described by its initial configuration and the sequence of moves on thepuzzle. The first 5 lines of each puzzle description are the startingconfiguration. Subsequent lines give the sequence of moves.
The first line of the frame display corresponds to thetop line of squares in the puzzle. The other lines follow in order. The emptyposition in a frame is indicated by a blank. Each display line contains exactly5 characters, beginning with the character on the leftmost square (or a blankif the leftmost square is actually the empty frame position). The display lineswill correspond to a legitimate puzzle.
The sequence of moves is represented by a sequence of As,Bs, Rs, and Ls to denote which square moves into the empty position. A denotesthat the square above the empty position moves; B denotes that the square belowthe empty position moves; L denotes that the square to the left of the emptyposition moves; R denotes that the square to the right of the empty positionmoves. It is possible that there is an illegal move, even when it isrepresented by one of the 4 move characters. If an illegal move occurs, thepuzzle is considered to have no final configuration. This sequence of moves maybe spread over several lines, but it always ends in the digit 0. The end ofdata is denoted by the character Z.
Output for each puzzle begins with an appropriatelylabeled number (Puzzle #1,Puzzle #2, etc.). If the puzzle has no finalconfiguration, then a message to that effect should follow. Otherwise thatfinal configuration should be displayed.
Format each line for a final configuration so that thereis a single blank character between two adjacent letters. Treat the emptysquare the same as a letter. For example, if the blank is an interior position,then it will appear as a sequence of 3 blanks - one to separate it from thesquare to the left, one for the empty position itself, and one to separate itfrom the square to the right.
Separate output from different puzzle records by oneblank line.
Note: Thefirst record of the sample input corresponds to the puzzle illustrated above.
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
Puzzle #1:
T R G S J
X O K L I
M D V B N
W P A E
U Q H C F
Puzzle #2:
A B C D
F G H I E
K L M N J
P Q R S O
T U V W X
Puzzle #3:
This puzzle has no final configuration.
代码:
#include<algorithm>//swap函数
using namespace std;
char ch[5][5];//必须声明为全局变量
int x=0,y=0;//定位空格的坐标
int number=1;//输入次数的标记
bool input1();//输入5*5矩阵的函数
bool seek();//明确X 和Y值的函数
bool input2();//输入命令的函数
bool handle(char c);//根据命令处理的函数
void output1();//有解时的输出函数
void output2();//无解时的输出函数
int main()
{
while(input1())
{
seek();
if(input2())
{
output1();
}
else
{
output2();
}
}
}
bool input1()
{
while(cin.getline(&ch[0][0],10)&&ch[0][0]!='Z'&&ch[0][1]!=0)
{
if(ch[0][0]==0)
{
continue;
/*解释:因为输入命令时当程序读到0时结束读入,也就说,输入命令后,缓冲区还有一个换行符(每次输入命令都有),接下来进行第二次运行时,cin.getline(&ch[0][0],10)首先看到的是换行符,所以第一行相当于没读入新的字符串,还是上一次运行后的字符串,所以对于这种情况,应当单独考虑*/
}
else
{
for(int i=1; i<5; i++)
{
cin.getline(&ch[i][0],10);
}
return true;
}
}
returnfalse;
}
bool seek()
{
for(int i=0;i<5; i++)
{
for(intj=0; j<5; j++)
{
if(ch[i][j]==' ')
{
x=i;
y=j;
return true;
}
}
}
}
bool input2()
{
char c;
while(cin.get(c)&&c!='0')//使得必须读到0字符才停止读入
{
if(c=='\n')//若读入的是换行字符,不做处理
{}
elseif(!handle(c))//不是换行字符,处理;若非法操作,清空0字符以前的字符
{
while(cin.get(c)&&c!='0');
return false;
}
}
return true;
}
bool handle(char c)
{
switch(c)
{
case 'A':
if(x>=1)
{
swap(ch[x][y],ch[x-1][y]);
--x;//一定别忘记交换后调整坐标
return true;
}
else
{
return false;
}
case 'B':
if(x<=3)
{
swap(ch[x][y],ch[x+1][y]);
++x;
return true;
}
else
{
return false;
}
case 'R':
if(y<=3)
{
swap(ch[x][y],ch[x][y+1]);
++y;
return true;
}
else
{
return false;
}
case 'L':
if(y>=1)
{
swap(ch[x][y],ch[x][y-1]);
--y;
return true;
}
else
{
return false;
}
}
}
void output1()
{
if(number!=1)
{
cout<<endl;//第一个单独输出
}
cout<<"Puzzle#"<<number<<":"<<endl;
number++;
for(int i=0;i<5; i++)
{
for(intj=0; j<4; j++)
{
cout<<ch[i][j]<<" ";
}
cout<<ch[i][4]<<endl;
}
}
void output2()
{
if(number!=1)
{
cout<<endl;
}
cout<<"Puzzle#"<<number<<":"<<endl;
number++;
cout<<"This puzzle has no final configuration."<<endl;
}