Puzzle
Time limit: 3.000 seconds
Puzzle
A children's puzzle that was popular 30 years ago consisted of a 5x5 framewhich contained 24 small squares of equal size. A unique letter of thealphabet was printed on each small square. Since there were only 24 squareswithin the frame, the framealso 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 tothe right, to the left, above, or below the empty position. The object of thepuzzle was to slide squares into the empty position so that the framedisplayed the letters in alphabetical order.
The illustration below represents a puzzle in its original configuration andin its configuration after the 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 configurationsand sequences of moves.
Input
Input for your program consists of several puzzles. Each is described by itsinitial configuration and the sequence of moves on the puzzle. The first 5lines of each puzzle description are the starting configuration. Subsequentlines give the sequence of moves.
The first line of the frame display corresponds to the top line of squaresin the puzzle. The other lines follow in order. The empty position in a frameis indicated by a blank. Each display line contains exactly 5 characters,beginning with the character on the leftmost square (or a blank ifthe 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 todenote which square moves into the empty position. A denotes that the squareabove the empty position moves; B denotes that the square below the emptyposition 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 movesmay be spread over several lines, but it always ends in the digit 0. Theend 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, thena message to that effect should follow. Otherwise that final configurationshould be displayed.
Format each line for a final configuration so that there is a single blankcharacter between two adjacent letters. Treat the empty square the sameas a letter. For example, if the blank is an interior position, then it willappear as a sequence of 3blanks - one to separate it from the square to the left, one for theempty 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 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.
谜题
有一个 5*5 的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A,B,L,R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指令序列(以数字 0 结束),输出指令执行完毕后的网格。如果有非法指令,应输出“This puzzle has no final configuration."。
【分析】
(分析过程附加在程序注释中)
用java语言编写程序,代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char[][] in = new char[5][5];//存储网格字符
char[] arr;//指令序列
//空格的位置 (blankPosX, blankPosY)
int blankPosX = 0, blankPosY = 0, x, y;
int puzzleCase = 0;//谜题的序号
boolean first = true;//用于判断是否为第一谜题答案的输出,如果是则不能输出换行,否则输出换行
while(input.hasNext()) {
String str;
//读取换行(为什么这样做的原因看下面注释)
if(puzzleCase != 0)
str = input.nextLine();
boolean ans = true;
//初始化网格
str = input.nextLine();
if(str.equals("Z"))
break;
for(int i = 0; i < 5; i++) {
in[0][i] = str.charAt(i);
if(in[0][i] == ' ') {
blankPosX = 0;
blankPosY = i;
}
}
for(int i = 1; i < 5; i++) {
str = input.nextLine();
for(int j = 0; j < 5; j++) {
in[i][j] = str.charAt(j);
if(in[i][j] == ' ') {
blankPosX = i;
blankPosY = j;
}
}
}
//读取指令序列
boolean isEnd = false;
String temp = "";
while(!isEnd) {
//由于此处使用next()进行读取,当我们输入指令后进行换行时,换行符未被读取
//当重新开始新谜题的网格读取时,使用nextLine()方法,它将会读取换行
//这也是为什么在程序一开始要先用nextLine()方法读取一下的原因。
//当然第一个谜题时,我们并不需要读取换行。
temp = temp + input.next();
if(temp.charAt(temp.length() - 1) == '0')
isEnd = true;
}
arr = temp.toCharArray();//将字符串指令转化为字符数组
for(int i = 0; i < arr.length; i++) {
if(arr[i] == '0')
break;
//非法指令:数组越界。我们只要先判断当发生某种交换时是否将发生数组越界,就可以知道该指令是否非法
if(arr[i] == 'A') {
if(blankPosX == 0) {
ans = false;
break;
}
else {
swap(in, blankPosX, blankPosY, blankPosX - 1, blankPosY);
blankPosX = blankPosX - 1;
}
}
else if(arr[i] == 'B') {
if(blankPosX == 4) {
ans = false;
break;
}
else {
swap(in, blankPosX, blankPosY, blankPosX + 1, blankPosY);
blankPosX = blankPosX + 1;
}
}
else if(arr[i] == 'L') {
if(blankPosY == 0) {
ans = false;
break;
}
else {
swap(in, blankPosX, blankPosY, blankPosX, blankPosY - 1);
blankPosY = blankPosY - 1;
}
}
else if(arr[i] == 'R'){
if(blankPosY == 4) {
ans = false;
break;
}
else {
swap(in, blankPosX, blankPosY, blankPosX, blankPosY + 1);
blankPosY = blankPosY + 1;
}
}
}
if(first)
first = false;
else
System.out.println();
System.out.println("Puzzle #" + (++puzzleCase) + ":");
if(ans == false)
System.out.println("This puzzle has no final configuration.");
else {
for(int i = 0; i < 5; i++) {
System.out.print(in[i][0]);
for(int j = 1; j < 5; j++)
System.out.print(" " + in[i][j]);
System.out.println();
}
}
}
}
//交换字符的位置
public static void swap(char[][] arr, int x1, int y1, int x2, int y2) {
char temp = arr[x1][y1];
arr[x1][y1] = arr[x2][y2];
arr[x2][y2] = temp;
}
}