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 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 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.
TRGSJ 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.
题意:
模拟题,给定一个5*5的字母矩阵,按照给定的指令,移动字母,成功输出最后指令即可,否则输出This puzzle has no final configuration.
解题思路:
1. 先读入矩阵,然后要把全部指令读入,进行模拟判定
2. 注意要用getline()读入每一行,因为可能会有空格,cin读入会分隔
错误分析:
1. 开始犯了一个错误,没有把全部指令读入,就去判断。比如说对于指令AAAAABBRRRLL0,其可以是
AAAA
ABBRR
RL
L
0
这种换行形式,所以一定要读到0为止,即把全部指令保存下来,再去判断
2. 要用cin.get()读一个enter产生的换行,因为回车键生成的换行符留存了输入队列中,后面的getline(cin,str)看到换行符后,将认为是一个空行。
3. Runtime error 最后一直有这个错误,后来发现调用strchr()函数产生的问题
代码如下:
#include
#include
#include
#include
using namespace std;
char puzzle[10][10];
int r0,c0;//记录空格的位置
const char* turns = "ABLR";
int dx[] = {-1,1,0,0};
int dy[] = {0,0,-1,1};
void read(int row, string str)
{
for(int j=0;j<5;j++){
puzzle[row][j] = str[j];
if(str[j]==' ') { r0 = row; c0 = j;}
}
}
bool move_p(char turn)
{
int flag = 1;
//int t = (strchr(turns,turn)-turns);
int t;
if(turn == 'A') t =0;
else if(turn == 'B') t =1;
else if(turn == 'L') t =2;
else if(turn == 'R') t =3;
int x,y;
x = r0 + dx[t]; y = c0 + dy[t];
if(x>=0&&x<=4&&y>=0&&y<=4){
puzzle[r0][c0] = puzzle[x][y];
puzzle[x][y] = ' ';
r0 = x; c0 =y;
}
else flag = 0;
if(flag) return true;
else return false;
}
int main()
{
//freopen("227.txt","r",stdin);
string str,order;
int icase = 0;
while(1)
{
if(icase!=0) cin.get();
getline(cin,str); //cin>>str;
if(str[0]=='Z') break;
if(icase!=0) cout<>temp;
order += temp;
len = temp.length();
if(temp[len-1]=='0') break;
else continue;
}
ok = 1;len = order.length();
for(int i=0;i