UVA-227-Puzzle

本文介绍了一道ACM编程题目,涉及5x5拼图的模拟移动过程。通过解读题目要求,提供了完整的C++代码实现,并针对输入输出及非法指令处理进行了详细说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本小白最近刚刚接触ACM,正在学习中(受虐中),希望各位指出错误。

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
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的网格,其中有一个格子空的,一共四种指令:A B L R分别表示把上下左右的相邻字母移到空格中。

每一个测试输入网格和指令以0结束,输出执行后的表格,当第一个输入Z时,结束输入。如果有非法指令,就输出This puzzle has no final configuration.

这道题是一个模拟表格移动,刚刚开始输入输出,都想了很久,最后采取了getchar();由于指令输入不连续可能中间存在回车等,我采用一个一个接收,存在一个数组中,但要注意细节,最后输出注意回车就行了。另外我做的这道题的叙述的第二个样列有问题,就是那个空格没有,让我以为有很大的坑,结果WA了很多次。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char m[8][8];
char mr[10050];
int main()
{
    char c;int cc=1;
    while(1)
    {
        memset(mr,'\0',sizeof(mr));
        if(cc>1)
            getchar();//接收以后每个数据的回车
        c=getchar();
        if(c=='Z')
            return 0;
        if(cc!=1)cout<<endl;
        m[1][1]=c;
        for(int i=1;i<=5;i++)
        {
            int j;
            for(j=1;j<=5;j++)
            {
                if(i==1&&j==1)
                    continue;//第一个不用输入
                m[i][j]=getchar();
            }
            getchar();
        }
        int x,y;
        for(int i=1;i<=5;i++)
        {
            for(int j=1;j<=5;j++)
            {
                if(m[i][j]==' ')
                {x=i;y=j;}
            }
        }
        int i=0;
        while(1)
        {
            char x;
            cin>>x;
            mr[i++]=x;
            if(x=='0')
                break;
        }
        int l=strlen(mr);bool flag=true;
        for(int i=0;i<l-1;i++)
        {
            char ch=mr[i],t;
            if(ch=='A')
            {
                if(x-1<1){flag=false;break;}
                t=m[x-1][y];m[x-1][y]=' ';m[x][y]=t;
                x=x-1;
            }
            else if(ch=='B')
            {
                if(x+1>5){flag=false;break;}
                t=m[x+1][y];m[x+1][y]=' ';m[x][y]=t;
                x=x+1;
            }
            else if(ch=='L')
            {
                if(y-1<1){flag=false;break;}
                t=m[x][y-1];m[x][y-1]=' ';m[x][y]=t;
                y=y-1;
            }
            else if(ch=='R')
            {
                if(y+1>5){flag=false;break;}
                t=m[x][y+1];m[x][y+1]=' ';m[x][y]=t;
                y=y+1;
            }
            else{flag=false;break;}
        }
        printf("Puzzle #%d:\n",cc++);
        if(flag==false)
            printf("This puzzle has no final configuration.\n");
        else
        {
            for(int i=1;i<=5;i++)
            {
                for(int j=1;j<=5;j++)
                {
                    if(j==1)
                        cout<<m[i][j];
                    else
                        cout<<" "<<m[i][j];
                }
                cout<<endl;
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值