【白书之路】 227 - Puzzle 模拟

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

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








UVA227 谜题 Puzzle 第一道难度为3的题,难度比之前的有提升,主要是两个坑点,第一个是如果空格是一行的最后一个,那么不会被输入,需要自己去补,第二个坑点是指令序列可能不止一行,还有就是输入需要注意一下 #include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define mk make_pair #define sz(x) ((int) (x).size()) #define all(x) (x).begin 阅读详情

相关推荐

UVa 227 Puzzle(小心输入输出!)

原题地址 https://vjudge.net/problem/UVA-227 题意如图 解题思路 本题是《算法竞赛入门经典》的习题3-5,题目本身非常简单,但是设了很多坑爹的陷阱,包括数据的输入、指令的输入、结果的输出,估计就是它主要的考点。 根据每个指令决定移动哪个行列位置的格子,当要移动的某个格子越界时则该指令非法。 总结一下我踩过的坑:...

weixin_30332705的博客 560

UVA 227 - Puzzle模拟

题目大意: 给出5*5的格子,其中有一个空格子,然后有一些命令。ABLR分别对应的上下左右,每一次移动两个格子都交换位置,命令以0结束。如果有非法操作则直接给出提示。 解题思路: 一道很好的模拟题,对于每个移动判一下是不是非法操作,不是非法操作的话模拟交换两个格子即可,代码很好理解,但是有很多坑点。 在输入的过程中,如果这一行最后一个格子是空格的话他是不会输入的,而是直接换行!!! 命令可能有多行,会有换行符,但一定是以0结束的。 输入输出!!调了一上午,输入我这边直接都用的getline(),cin是.

在读NLP硕士 312

uva227(谜题)

即可完成,核心问题是如何交换数值,一开始想直接用starti和startj记录每次变化后的空格的position(题中为0的位置),发现数组下标不能为变量,那用指针,用一个pos指针记录空格位置每次更新,perfect(dog)(--0--)!作者嫌弃打字母太慢,改用数字,该程序完成后只需再添加一个字典(如1对T,2对R等等)

2301_77701957的博客 216

UVA227 谜题 Puzzle 题解

题意翻译 有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4中指令:A,B,L,R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指令序列(以数字0结束),输出指令执行完毕后的网格。如果有非法指令,应输出“This puzzle has no final configuration.”例如,左图中执行ARRBBL0后,效果如右图所示。 输入输出样例 输入 #1 TRGSJ XDOKI M VLN WPABE UQHCF ARRBBL0 ABCDE FGHIJ KL

m0_63486615的博客 820

forensicscontest测试一

3、对于即时通信软件IM而言,大部分情况,由于会涉及到外网通信,所以一般都会使用公网IP地址,那么由此,可以初步尝试分析前述的23数据包,由公网IP地址进行搜索:https://tool.lu/ip/index.html,搜索IP地址:64.12.24.50,得知IP归属于美国AOL美国在线公司。Ann Dercover是一个公司的雇员,公司怀疑Ann是其他公司的商业间谍来窃取公司的秘方(secret recipe),故对其的行为进行了关注。6、应用后,原本的很多加密协议内容都已经被解析了,继续分析。

tammyztc的博客 685

uva227 (我tm破防了!!!)

首先说一下我不讲这个,因为我本身vs没有运行出来正确结果,在第二个输入中我输入的这个多出一个空格,我就算是再vj里面用别人的代码也是有问题,我就想算了不较真了,应该是vs的环境问题放在了vj这里说我超时我也就先放放吧等我过一阵子再从新检查这个代码。这篇文章就当是给我提提醒了,纱布题目我ccccccccccccccccccccccc。本人写了小十天就干这个(我太菜了那个紫皮书出这么恶心的题本身代码逻辑并不难,我也写出来了但是就算有问题我ccccccccccccccc)

2301_80844586的博客 319

UVa227:谜题(Puzzle

题目: 有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A, B, L, R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指令序列(以数字0结束),输出指令执行完毕后的网格。如果有非法指令,应输出“This puzzle has no final configuration.”如下图所示:分别为执行ARRBBL0前、后的效果图: Sample...

yishi_c的博客 739

UVA-227

UVA-227 这道题写了有一个半小时,里面除了两三分钟写移动的代码,其他全都在处理输入输出格式,简直吐血。自己习惯STL,但是这次训练中刻意没有用string处理square,代码看起来好丑。 //#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string> #include<vector> #include<cstring> using namespace std; bool mov

桂圆儿的仓库 254

UVa - 227 Puzzle

UVa - 227 Puzzle一道水题 第一次是wrong answer,没有什么好说 第二次之后全是PE,真是醉了,这道题有两个坑 第一个坑是样例给的数是没有空格 TRGSJ XDOKI M VLN <<——这里的是真空行 WPABE UQHCF ARRBBL0 ABCDE FGHIJ

平行空间 594

UVA232 纵横字谜的答案 Crossword Answers 题解

题意翻译 输入一个r 行c 列(1<r,.c<10) 的网格,黑格用“*”每个白格都填有一个字母。如果一个白格的左边相邻位置或者上边相邻位置没有白格(可能是黑格,也可能出了网格边界),则称这个白格是一个起始格 你的任务是找出网格中所有所有横向单词(Across)。.这些单词必须从一个起始格(左边是黑格,或是第一列) 开始,向右延伸到一个黑格的左边或者整个网格的最右列。然后找出所有竖向单词(Down)。 这些单词必须从一个起始格(上边是黑格,或是第一行) 开始,向下延伸到一个黑格的上边或者整个网格

m0_63486615的博客 552

UVa 227 - Puzzle

题目大意:有一个5*5的方阵,其中有一个空格,可以把上下左右的字母移过去。一共有四种操作,ABLR——上下左右,0表示操作结束。多组输入,输入Z结束。先给出初始方阵(注意如果直接拷贝样例数据,末尾的空格是不存在的),再给出一个操作字符串(注意可能不在一行),要求输出变换后的方阵(注意Puzzle前有一个空行),如果有非法操作(方阵移动越界等),就输出“This puzzle has no fina...

Euthanazia的博客 383

uva 227

这道题是道水题,至于为什么我做了这么长时间,完全是因为我自己能力的不足。从昨天下午四点做到现在才A出来,改写了无数次。 这道题的问题在于:你不能一边读指令一边走。你一边读指令一边走的话应该会把一些指令留在缓冲区中,导致一系列问题,我在昨晚就注意到了这个问题,今早在重写这个题时,将数种清空cin缓存的方法都堆上了,但是还是不能解决这个问题。希望各位看官能够帮助我解决这个问题 错误代码:#incl

qq_35859033的博客 373

UVa 227 Puzzle (紫书上的题,模拟)

Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small

AC_Dreameng 4710

Puzzle(模拟)

A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there...

starlet_kiss的博客 553

UVA_227 - Puzzle

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 sq

wowowoc的专栏 4055

Uva227.Puzzle

Puzzle                Time limit: 3.000 seconds   A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter

ACM学习交流 510

UVA227 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 therew

海岛Blog 2723

谜题(Puzzle)

Puzzle Time limit: 3.000 seconds  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

梦空间 1802
上一篇: android support 支持包 使用
下一篇: 基于过程的sin函数的计算
colorfulshark
博客等级 码龄15年 631粉丝 459原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值