【白書之路】 227
Puzzle
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
Sample Output
就是一道模擬題,相信大家都或多或少的聽說過這個遊戲,有的是拼圖,不過,這道題並不讓我們自己去解,而是隻要模擬方塊移動的過程,然後將移動之後的狀態輸出就可以了,有幾個地方需要注意:
1.在字串讀入的時候,建議大家不要使用gets(),這個函式是有風險的,可能會造成記憶體洩漏,在較新的標準中已經捨棄這個函數了,因此無論是做競賽還是以後工作中寫程式碼,使用其都會導致不可預知的風險,建議使用fgets();
2.因為指令可能不出現在同一行,因此需要事先將所有的指令讀入,使用getchar()讀入,我一開始是讀入一個指令執行一次,如果錯誤直接退出,這裡就會導致輸入緩衝區中還存有內容,在讀下一組測試資料時會造成影響,我於是使用fflush()清空緩衝區,結果超時,後來將所有指令讀入後,還是有一個回車載緩衝區中,因此需要使用getchar函式讀出這個字元,從而保證緩衝區是空的;
3.每兩組測試資料之間用空行分開是一種常見的格式要求,如果測試資料數量給定比較容易,但是在測試資料量不定的時候,我們可以使用計數器,除了第一組結果,其他的結果都在其之前加一個空行。
#include <iostream>
#include <stdio.h>
using namespace std;
const int ABOVE=0;
const int BELOW=1;
const int LEFT=2;
const int RIGHT=3;
int space_r,space_c;
int temp_r,temp_c;
int dir_r[4]= {-1,1,0,0};
int dir_c[4]= {0,0,-1,1};
char cmd[1010];
int cmd_count;
char square[10][10];
int judge_range()
{
if(temp_r>=0&&temp_r<5&&temp_c>=0&&temp_c<5)
return 1;
else
return 0;
}
int move_space(int dir)
{
temp_r=space_r+dir_r[dir];
temp_c=space_c+dir_c[dir];
if(judge_range())
{
square[space_r][space_c]=square[temp_r][temp_c];
square[temp_r][temp_c]=' ';
space_r=temp_r;
space_c=temp_c;
return 1;
}
else
return 0;
}
void show_square()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%c",square[i][j]);
if(j!=4)
printf(" ");
}
printf("\n");
}
}
int main()
{
int i,j,cas=1;
char op;
bool flag;
while(1)
{
flag=true;
cmd_count=0;
fgets(square[0],10,stdin);//讀取第一行
if(square[0][0]=='Z')
break;
for(i=1; i<5; i++) //讀取後四行
{
fgets(square[i],10,stdin);
}
for(i=0; i<5; i++) //找到空格位置
{
for(j=0; j<5; j++)
{
if(square[i][j]<'A'||square[i][j]>'Z')
{
space_r=i;
space_c=j;
i=10,j=10;
}
}
}
//printf("r=%d,c=%d\n",space_r,space_c);
while((cmd[cmd_count++]=getchar())!='0')
{
}
getchar();
for(i=0;i<cmd_count&&flag;i++)//移動操作
{
switch(cmd[i])
{
case 'A':
flag=move_space(ABOVE);
break;
case 'B':
flag=move_space(BELOW);
break;
case 'L':
flag=move_space(LEFT);
break;
case 'R':
flag=move_space(RIGHT);
break;
default:
continue;
}
}
if(cas!=1)
printf("\n");
printf("Puzzle #%d:\n",cas++);
if(flag)
show_square();
else
printf("This puzzle has no final configuration.\n");
}
return 0;
}