这题的坑在于输出格式。。。
原题:
A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smallsquares of equal size. A unique letter of the alphabet was printed on each small square. Since therewere only 24 squares within the frame, the frame also contained an empty position which was the samesize as a small square. A square could be moved into that empty position if it were immediately to theright, to the left, above, or below the empty position. The object of the puzzle was to slide squaresinto 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 afterthe following sequence of 6 moves:
1) The square above the empty position moves.
2) The square to the right of the empty position moves.
3) The square to the right of the empty position moves.
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 position moves.
Write a program to display resulting frames given their initial configurations and sequences of moves.
Input
Input for your program consists of several puzzles. Each is described by its initial configuration andthe sequence of moves on the puzzle. 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 the top line of squares in the puzzle. The otherlines follow in order. The empty position in a frame is indicated by a blank. Each display line containsexactly 5 characters, beginning with the character on the leftmost square (or a blank if the leftmostsquare is actually the empty frame position). The display lines will correspond to a legitimate puzzle.
The sequence of moves is represented by a sequence of As, Bs, Rs, and Ls to denote which squaremoves into the empty position. A denotes that the square above the empty position moves; B denotesthat the square below the 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 position moves. It is possible thatthere is an illegal move, even when it is represented by one of the 4 move characters. If an illegal moveoccurs, the puzzle is considered to have no final configuration. This sequence of moves may be spreadover several lines, but it always ends in the digit 0. The end of data is denoted by the character Z.
Output
Output for each puzzle begins with an appropriately labeled number (Puzzle #1, Puzzle #2, etc.). Ifthe puzzle has no final configuration, then a message to that effect should follow. Otherwise that finalconfiguration should be displayed.
Format each line for a final configuration so that there is a single blank character between twoadjacent letters. Treat the empty square the same as a letter. For example, if the blank is an interiorposition, then it will appear as a sequence of 3 blanks — one to separate it from the square to the left,one for the empty position itself, and one to separate it from the square to the right.
Separate output from different puzzle records by one blank line.
Note: The first record of the sample input corresponds to the puzzle illustrated above.
Sample Input
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
S
Sample Output
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 <iostream>
#include <cstring>
#include <cstdio>
#define maxn 8
char con[5][maxn];
char mov[100];
char tmpmov[20];
int main() {
using namespace std;
int flag, T= 0;
int bi, bj;
char cmov;
cin.getline(con[0], maxn);
while (con[0][0]!='Z') {
T++;
flag= 1;
for (int i= 1; i<5; i++) {
cin.getline(con[i], maxn);
}
for (int i= 0; i<5; i++) {
int j;
for (j= 0; j<5; j++)
if(con[i][j]==' ' && i<5 && j<5) {bi= i; bj= j; break;}
if(con[i][j]==' ' && i<5 && j<5) break;
}
memset(mov, 0, sizeof(mov)); // 清空指令集合
cin.getline(tmpmov, 20); // 读取移动指令
while (tmpmov[strlen(tmpmov)-1]!='0') {
strcat(mov, tmpmov);
cin.getline(tmpmov, 20);
}
strcat(mov, tmpmov);
if(T>1) putchar('\n');
printf("Puzzle #%d:\n", T);
for (int i= 0; mov[i]!='0'; i++) {
cmov= mov[i];
if (cmov=='A') { // 向上
if(bi==0){
printf("This puzzle has no final configuration.\n");
flag= 0; break;
} // 越界判断
else{
con[bi][bj]= con[bi-1][bj];
con[bi-1][bj]= ' ';
bi--;
}
}
else if (cmov=='B'){ // 向下
if(bi==4){
printf("This puzzle has no final configuration.\n");
flag= 0; break;
} // 越界判断
else{
con[bi][bj]= con[bi+1][bj];
con[bi+1][bj]= ' ';
bi++;
}
}
else if (cmov=='L'){ // 向左
if(bj==0){
printf("This puzzle has no final configuration.\n");
flag= 0; break;
} // 越界判断
else{
con[bi][bj]= con[bi][bj-1];
con[bi][bj-1]= ' ';
bj--;
}
}
else // 向右
{
if(bj==4){
printf("This puzzle has no final configuration.\n");
flag= 0; break;
} // 越界判断
else{
con[bi][bj]= con[bi][bj+1];
con[bi][bj+1]= ' ';
bj++;
}
}
}
if(flag==1){
for (int i= 0; i<5; i++) {
printf("%c %c %c %c %c\n", con[i][0], con[i][1], con[i][2], con[i][3], con[i][4]);
}
}
cin.getline(con[0], maxn);
}
return 0;
}