题目
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:
- The square above the empty position moves.
- The square to the right of the empty position moves.
- The square to the right of the empty position moves.
- The square below the empty position moves.
- The square below the empty position moves.
- 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 and the sequence of moves on the puzzle. The first 5 lines of each puzzle description are the starting configuration. 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 other lines follow in order. The empty position in a frame is indicated by a blank. Each display line contains exactly 5 characters, beginning with the character on the leftmost square (or a blank if the leftmost square 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 square moves into the empty position. A denotes that the square above the empty position moves; B denotes that the square below the empty position moves; L denotes that the square to the left of the empty position moves; R denotes that the square to the right of the empty position moves. It is possible that there is an illegal move, even when it is represented by one of the 4 move characters. If an illegal move occurs, the puzzle is considered to have no final configuration. This sequence of moves may be spread over 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.). If the puzzle has no final configuration, then a message to that effect should follow. Otherwise that final configuration should be displayed.
Format each line for a final configuration so that there is a single blank character between two adjacent letters. Treat the empty square 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 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
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 FPuzzle #2:
* A B C D
F G H I E
K L M N J
P Q R S O
T U V W XPuzzle #3:
This puzzle has no final configuration.
注:输出中使用了*来代替目标空格。
我的解读
读入
首先可以看见这题没有像往常一样给出题目数量N,而是通过特定字符('Z')来结束。这个问题很好解决。只需每次读入数据时判断第一个输入字符是不是'Z'就能解决。更具体一点,可以在一个循环的条件先读入第一行字符串,然后进行判断,若符合则跳出循环即可结束程序,否则继续运行。(当然,亦可以先用getchar读入第一个字符,然后判断)
然后是读入的字符含有空格的问题。这意味着不可以使用scanf来进行读入,只能使用fgets来读入一行 或者 getchar来一个一个读入。(但getchar会读入 \n 所以需要稍作处理)
之后需要读入的是操作指令,结束符同样进行了替换('0'),同时操作指令中可能含有 \n 。就是说只读入字符串的做法行不通,需要特殊处理。(笔者的做法是用getchar每读入一个字符进行一次操作,但需要处理命令结束后的 \n)
输出
输出的要求也比往常苛刻,但是不难处理。
第一个是输出当前题号,只需用一个变量计数即可。
然后输出答案只需每一行的第一个字符或者最后一字符不输出空格即可。
之后是每一题之间都要有一行间隔,但是最后一题时不能有空白行。也比较容易实现。
核心考点
输入函数的灵活运用,输出格式的掌握。
代码
#include <stdio.h>
#include <string.h>
#define MAXN 10000
int main() {
char map[5][7];
int kase = 0;
while (fgets(map[0], 7, stdin)) {
if (map[0][0] == 'Z')
break;
for (int i = 1; i < 5; i++)
fgets(map[i], 7, stdin);
int x, y;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (map[i][j] == ' ') {
x = i;
y = j;
}
int x1 = x, y1 = y;
bool flag = true;
char order;
while ((order = getchar()) != '0') {
switch (order) {
case 'A':
x1--; break;
case 'B':
x1++; break;
case 'L':
y1--; break;
case 'R':
y1++; break;
default:
break;
}
if (x1 > 4 || x1 < 0 || y1>4 || y1 < 0)
flag = false;
else {
map[x][y] = map[x1][y1];
map[x1][y1] = ' ';
x = x1; y = y1;
}
}
getchar();
if (kase++) printf("\n");
printf("Puzzle #%d:\n", kase);
if (!flag)
printf("This puzzle has no final configuration.\n");
else {
for (int i = 0; i < 5; ++i) {
printf("%c", map[i][0]);
for (int j = 1; j < 5; ++j)
printf(" %c", map[i][j]);
printf("\n");
}
}
}
return 0;
}