【白书之路】 227 - Puzzle 模拟

来源:互联网 发布:亿云易购网络传销窝点 编辑:程序博客网 时间:2024/05/22 05:01

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;}








0 0
原创粉丝点击