227—puzzle

A children's puzzle that waspopular 30 years ago consisted of a 5x5 frame which contained 24 small squaresof equal size. A unique letter of the alphabet was printed on each smallsquare. Since there were only 24 squares within the frame, the frame also containedan empty position which was the same size as a small square. A square could bemoved into that empty position if it were immediately to the right, to theleft, above, or below the empty position. The object of the puzzle was to slidesquares into the empty position so that the frame displayed the letters inalphabetical order.

The illustration below representsa puzzle in its original configuration and in its configuration after thefollowing sequence of 6 moves:

1)                           The square above the empty position moves.

2)                           The square to the right of the empty positionmoves.

3)                           The square to the right of the empty positionmoves.

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 positionmoves.

Write a program to display resulting frames given theirinitial configurations and sequences of moves.

Input

Input for your program consists of several puzzles. Eachis described by its initial configuration and the sequence of moves on thepuzzle. 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 thetop line of squares in the puzzle. The other lines follow in order. The emptyposition in a frame is indicated by a blank. Each display line contains exactly5 characters, beginning with the character on the leftmost square (or a blankif the 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 to denote which square moves into the empty position. A denotesthat the square above the empty position moves; B denotes that the square belowthe 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 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 moves maybe spread over several lines, but it always ends in the digit 0. The end ofdata is denoted by the character Z.

Output

Output for each puzzle begins with an appropriatelylabeled number (Puzzle #1,Puzzle #2, etc.). If the puzzle has no finalconfiguration, then a message to that effect should follow. Otherwise thatfinal configuration should be displayed.

Format each line for a final configuration so that thereis a single blank character between two adjacent letters. Treat the emptysquare 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 thesquare to the left, one for the empty position itself, and one to separate itfrom the square to the right.

Separate output from different puzzle records by oneblank line.

Note: Thefirst record of the sample input corresponds to the puzzle illustrated above.

SampleInput

TRGSJ

XDOKI

M VLN

WPABE

UQHCF

ARRBBL0

ABCDE

FGHIJ

KLMNO

PQRS

TUVWX

AAA

LLLL0

ABCDE

FGHIJ

KLMNO

PQRS

TUVWX

AAAAABBRRRLL0

Z

SampleOutput

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.

代码:

#include<iostream>  

#include<algorithm>//swap函数

using namespace std;

char ch[5][5];//必须声明为全局变量

int x=0,y=0;//定位空格的坐标

int number=1;//输入次数的标记

bool input1();//输入5*5矩阵的函数

bool seek();//明确X 和Y值的函数

bool input2();//输入命令的函数

bool handle(char c);//根据命令处理的函数

void output1();//有解时的输出函数

void output2();//无解时的输出函数

int main()

{

   while(input1())

    {

        seek();

       if(input2())

        {

           output1();

        }

        else

        {

           output2();

        }

    }

}

bool input1()

{

   while(cin.getline(&ch[0][0],10)&&ch[0][0]!='Z'&&ch[0][1]!=0)

    {

       if(ch[0][0]==0)

        {

           continue;

/*解释:因为输入命令时当程序读到0时结束读入,也就说,输入命令后,缓冲区还有一个换行符(每次输入命令都有),接下来进行第二次运行时,cin.getline(&ch[0][0],10)首先看到的是换行符,所以第一行相当于没读入新的字符串,还是上一次运行后的字符串,所以对于这种情况,应当单独考虑*/

        }

        else

        {

           for(int i=1; i<5; i++)

            {

               cin.getline(&ch[i][0],10);

            }

           return true;

        }

    }

    returnfalse;

}

bool seek()

{

    for(int i=0;i<5; i++)

    {

        for(intj=0; j<5; j++)

        {

           if(ch[i][j]==' ')

            {

               x=i;

               y=j;

               return true;

            }

        }

    }

}

bool input2()

{

    char c;

    while(cin.get(c)&&c!='0')//使得必须读到0字符才停止读入

    {

       if(c=='\n')//若读入的是换行字符,不做处理

        {}

        elseif(!handle(c))//不是换行字符,处理;若非法操作,清空0字符以前的字符

        {

           while(cin.get(c)&&c!='0');

           return false;

        }

    }

    return true;

}

bool handle(char c)

{

    switch(c)

    {

    case 'A':

       if(x>=1)

        {

           swap(ch[x][y],ch[x-1][y]);

           --x;//一定别忘记交换后调整坐标

           return true;

        }

        else

        {

           return false;

        }

    case 'B':

       if(x<=3)

        {

           swap(ch[x][y],ch[x+1][y]);

            ++x;

           return true;

        }

        else

        {

           return false;

        }

    case 'R':

       if(y<=3)

        {

           swap(ch[x][y],ch[x][y+1]);

            ++y;

           return true;

        }

        else

        {

           return false;

        }

    case 'L':

       if(y>=1)

        {

           swap(ch[x][y],ch[x][y-1]);

            --y;

           return true;

        }

        else

        {

           return false;

        }

    }

}

void output1()

{

   if(number!=1)

    {

       cout<<endl;//第一个单独输出

    }

   cout<<"Puzzle#"<<number<<":"<<endl;

    number++;

    for(int i=0;i<5; i++)

    {

        for(intj=0; j<4; j++)

        {

           cout<<ch[i][j]<<" ";

        }

       cout<<ch[i][4]<<endl;

    }

}

void output2()

{

   if(number!=1)

    {

       cout<<endl;

    }

   cout<<"Puzzle#"<<number<<":"<<endl;

    number++;

   cout<<"This puzzle has no final configuration."<<endl;

}

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值