UVa227 Puzzle

UVa 227 Puzzle(谜题) UVa 227 Puzzle(谜题) UVa题目链接 题目名称:谜题 题目描述: 有一个55的网格,其中恰好有一个格子是空的,其他格子各有一个字母,一共有四种指令:A,B,L,R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指令序列(分别以数字0结束),输出指令执行完毕后的网格。如果有非法指令,应输出"This puzzle has no final configura 阅读详情

这题的坑在于输出格式。。。

原题:

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
S

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.
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 8
char con[5][maxn];
char mov[100];
char tmpmov[20];
int main() {
    using namespace std;
    int flag, T= 0;
    int bi, bj;
    char cmov;
    cin.getline(con[0], maxn);
    while (con[0][0]!='Z') {
        T++;
        flag= 1;
        for (int i= 1; i<5; i++) {
            cin.getline(con[i], maxn);
        }
        for (int i= 0; i<5; i++) {
            int j;
            for (j= 0; j<5; j++)
                if(con[i][j]==' ' && i<5 && j<5)  {bi= i;  bj= j;  break;}
            if(con[i][j]==' ' && i<5 && j<5)  break;
        }
        memset(mov, 0, sizeof(mov));  // 清空指令集合
        cin.getline(tmpmov, 20);  // 读取移动指令
        while (tmpmov[strlen(tmpmov)-1]!='0') {
            strcat(mov, tmpmov);
            cin.getline(tmpmov, 20);
        }
        strcat(mov, tmpmov);
        if(T>1)  putchar('\n');
        printf("Puzzle #%d:\n", T);
        for (int i= 0; mov[i]!='0'; i++) {
            cmov= mov[i];
            if (cmov=='A') {       // 向上
                if(bi==0){
                    printf("This puzzle has no final configuration.\n");
                    flag= 0; break;
                }  // 越界判断
                else{
                    con[bi][bj]= con[bi-1][bj];
                    con[bi-1][bj]= ' ';
                    bi--;
                }
            }
            else if (cmov=='B'){      // 向下
                if(bi==4){
                    printf("This puzzle has no final configuration.\n");
                    flag= 0; break;
                }  // 越界判断
                else{
                    con[bi][bj]= con[bi+1][bj];
                    con[bi+1][bj]= ' ';
                    bi++;
                }
            }
            else if (cmov=='L'){      // 向左
                if(bj==0){
                    printf("This puzzle has no final configuration.\n");
                    flag= 0; break;
                }  // 越界判断
                else{
                    con[bi][bj]= con[bi][bj-1];
                    con[bi][bj-1]= ' ';
                    bj--;
                }
            }
            else      // 向右
            {
                if(bj==4){
                    printf("This puzzle has no final configuration.\n");
                    flag= 0; break;
                }  // 越界判断
                else{
                    con[bi][bj]= con[bi][bj+1];
                    con[bi][bj+1]= ' ';
                    bj++;
                }
            }
        }
        if(flag==1){
            for (int i= 0; i<5; i++) {
                printf("%c %c %c %c %c\n", con[i][0], con[i][1], con[i][2], con[i][3], con[i][4]);
            }
        }
        
        cin.getline(con[0], maxn);
    }
    return 0;
}



HDI盲埋孔设计规范:高速PCB信号完整性落地指南 盲埋孔是高密度互连(HDI)PCB实现高速信号完整性的关键技术,其本质是通过控制过孔stub长度、优化回流路径和提升布线密度,解决GHz级信号在传统通孔结构下出现的谐振、串扰与EMI超标问题。其设计核心在于叠层物理约束、电气参数建模与工艺能力协同,涉及stub长度计算、反焊盘尺寸推导、孔壁粗糙度Rz控制等关键指标。广泛应用于DDR4、PCIe Gen4、USB3.2等高速接口的PCB设计中,尤其在车载ADAS、5G基站及FPGA载板等对SI/PI要求严苛的场景中成为刚性门槛。 阅读详情

相关推荐

ais信号接收设备_国内首款基于软件无线电技术的AIS基站投入测试使用

星网宇达子公司北京凯盾环宇科技有限公司经过多年的潜心研发,成功研制出国内首台基于软件无线电(SDR)的CATON 9011 AIS基站。该基站采用软件无线电技术,利用软件数字技术代替传统的模拟电路,大大的降低了模拟器件的使用率,提高了基站的可靠性和使用年限。CATON 9011 AIS基站研制依据ITU-R M.1371-5、IEC 62320-1、IEC 61162-1等国际标准,兼容...

weixin_29057163的博客 2334

习题3-5 Puzzle UVa227

题目描述: 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. Sin...

elorole的博客 500

别再为Power BI瀑布图发愁了!用这个DAX公式+堆积柱状图,5分钟搞定现金流量表可视化

本文介绍了一种使用DAX公式和堆积柱状图快速实现Power BI现金流量表可视化的高效方法。通过详细的步骤和代码示例,帮助财务分析人员5分钟内完成专业级报表制作,解决传统瀑布图配置复杂、灵活性差的问题,显著提升工作效率。

weixin_28677005的博客 247

UVa Puzzle——C语言版本

《算法竞赛入门经典(第2版)》 作者:刘汝佳  第三章习题3-5 经典字母迷图(类似华容道),纯C语言实现 https://uva.onlinejudge.org//index.php?option=com_onlinejudge&Itemid=8&category=827 -------------------------------------------------E3-5.cpp--

Anntonnia的博客 967

Cadence Allegro实战:如何精准导入结构DXF文件避免安装偏差(附图层配置技巧)

本文详细讲解了在Cadence Allegro中精准导入结构DXF文件以避免安装偏差的实战技巧。核心在于导入前的单位校准、创建专用图层结构,以及基于坐标基准点的精确对齐方法。文章还分享了将DXF图形转化为正式板框和禁布区、高效管理图层以及向结构工程师导出DXF的完整流程,帮助PCB工程师确保设计与机械结构完美契合。

1a2s3d4f5g的博客 506

FMT(基于rtthread)开源代码编译 win10下编译环境搭建及编译

FMT(基于rtthread)开源代码编译 win10下编译环境搭建及编译 什么是Firmament? Firmament (FMT) 是一款基于模型设计 (Model-Based-Design, MBD) 的开源自驾仪,可被用来快速构建无人机,无人车,无人船,机器人等平台的控制系统。 系统主要由两部分构成. FMT-Firmware: 一个基于C/C++开发的稳定、高性能的嵌入式系统. FMT-Model: 一个基于MATLAB/Simulink开发的仿真框架以及算法库. 昨天得知FMT开源飞控项目已经开

weixin_45786595的博客 2873

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的博客 556

Unable to negotiate with XXXX port 22: no matching host key type found. Their offer: ssh-rsa,ssh-dss

打开ssh_config此文件,滑动到最下面,新增自己git主机地址(这里我直接默认*)执行命令 git clone git@xxxxxxxxxxxxx.git。代码仓库已经添加了ssh公钥之后,克隆代码到本地时就报了这个问题。首先是找到git安装目录(根据自己安装时选项,找到这个目录)再进入ssh目录,找到 ssh_config 配置文件。配置完成后,保存配置文件后再次去clone代码。直接复制下面这一段到文件中最底部位置即可。然后进入 etc目录,找到ssh目录。终于可以克隆下来了。

萌兔兔MMQ!! 3358

# UVA227 Puzzle

UVA227 Puzzle 题目大意是说给你一张图,再给你可能是多行的指令,使你移动空格。如果所有指令合法,输出最后的图。否则,输出。。。 难点:主要在输入。当空格在行末时,getline不会输入空格。 PE点:输入结束后,不打空行 #include <iostream> #include<stdio.h> #include<algorithm> #includ...

qq_43691704的博客 170

4.Simulink基础建模操作——加减乘除运算

加减乘除运算

FPGA/MATLAB学习教程/源码/项目合作开发 9550

5、模糊风险评估

本文介绍了基于模糊逻辑的风险评估方法,探讨了如何在不确定、不完整信息环境下进行高效的风险分析。文章涵盖了模糊集合与隶属度函数的基本概念、模糊推理系统的流程,并结合犯罪风险、金融风险和医疗诊断等实际案例,展示了模糊风险评估的应用价值和技术细节。此外,还介绍了模糊贝叶斯网络、模糊综合评价等进阶工具与方法,为多领域风险管理提供了理论支持和实践指导。

mango的博客 182

【UVA】 227 --- Puzzle

【UVA】 227 --- 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 smal...

来日浅谈的博客 487

【异常】OpenClaw 项目 `fetch failed` 报错问题排查与解决方案Response interrupted: TypeError: fetch failed

针对无超时控制、异常兜底缺失导致的请求中断,封装标准化的安全请求工具,一次性解决超时控制、异常捕获、日志埋点三大问题,全项目替换原生。:将项目运行的 Node.js 版本升级至 18.x 及以上 LTS 版本,使用 Node.js 内置的稳定版。实现的网络请求环节,包括但不限于:第三方开放接口调用、内部服务接口通信、跨域资源请求、外部配置拉取等场景。:针对无法升级 Node.js 版本的存量环境,通过社区稳定兼容包替换原生。类型异常,直接造成业务请求响应中断。第二步:业务代码改造,替换原生。

奶爸的编程之路,也就一周冷个三天 367

UVA227 Puzzle谜题

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 were only 24 squares within the frame, th

Hoolys 604

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

UVa227-Puzzle

UVa227-Puzzle思路:题意是有个5x5的板子上面有一个是空的,用空格表示,四种操作分别是ABRL,分别代表把空格上下右左的字母移动到空格处,然后给出一个初始状态,还有一系列操作输出最终状态,如果存在非法操作输出This puzzle has no final configuration.,非法操作就是越界的操作(比如空格已经在最右边了还要执行R就非法)。 这种模拟题一直都是最不愿意做的,

进击的ACMer!!! 366

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

UVa - 227 Puzzle

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

平行空间 596

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

UVa 227 Puzzle

UVa 227 Puzzle 这个题怕不是有毒,先是我第二个样例的空格死活不识别,后来我改成了&lt;’A’||&gt;’Z’才能标记的,好歹本地对了,提交上去WA了,然后我从网上搜AC代码,怎么看都觉得我没有问题,把AC代码粘到我本机上跑结果却是不对的,23333。 A children’s puzzle that was popular 30 years ago consisted of ...

mandiheyanyu的博客 177

习题3-5 UVA227 Puzzle(50行AC代码)

紫书刷题进行中,题解系列点这里 习题3-5 UVA227 Puzzle(50行AC代码) 思路分析 给定一个5*5的字母拼图(A-X),包含一个空格,再给定移动序列,以0表示结束,求最终的拼图,若非法则输出相应提示信息。 读入拼图后,先找出空格位置,再根据移动序列移动即可。 注意点 输入时getline可读入换行并丢弃它,cin遇到换行立即停止,不会丢弃,即下一个读取依旧是换行 输出时两个puz...

努力做个书呆儿 291

求教UVa227-Puzzle

【代码】求教UVa227-Puzzle。

2401_86561835的博客 251

UVA227 Puzzle

问题链接:UVA227 Puzzle。基础训练级的问题,用C语言编写程序。 问题简述:一个5×5的网格,一个格子是空的,其他格子各有一个字母,一共有四种指令:A,B,L,R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指令序列,指令序列以数字0结束,输出指令执行完毕后的网格。如果有非法指令,应输出"This puzzle has no fin...

weixin_33700350的博客 87

UVA - 227 Puzzle

UVA - 227 Puzzle 题目大意:给定一个5X5的格子,给定a~x的英文字母和一个空格,通过ABLR移动位置解题思路:二维数组交换元素 坑爹点:读cmd的时候要一个字符一个字符的读,不能gets()直接读,cmd可以分开的我居然没看到握草。。。另外gets()的使用依然很迷…

mydjrzal2的博客 352

UVA-227 Puzzle

题目大意:5×5 网格,有一个格子是空的,其他都是字母。一共四种指令:A,B,L,R,分别表示将空格上、下、左、右的字母移到空格中。输入网格和指令(以 0 结束),输出执行指令后的网格。 解题思路:二维数组模拟网格,用一个 char 类型的变量一边读一边执行,读入 0 结束。 注意:标记非法跳出后实际上并未读入完全,剩余指令会影响下一次的网格。读入时要十分注意!!! #defin

振丿Love 283

ParsecVDD虚拟显示器:安装配置、避坑指南与多屏生产力实践

虚拟显示驱动通过在系统内核层注册虚拟显示适配器,让操作系统识别一块逻辑上存在但物理上不存在的屏幕,从而在不增加硬件的情况下扩展桌面空间。其核心原理是驱动生成EDID信息并分配独立显示通道,使虚拟屏可单独设置分辨率、刷新率和缩放比例。这一技术为单屏设备提供了低成本的多屏工作流方案,广泛应用于远程协助、录屏直播、多窗口开发测试等场景。ParsecVDD作为典型实现,安装配置涉及驱动签名验证、显示拓扑调整和EDID覆盖等环节,合理调优后可显著提升多任务处理效率。

weixin_34152820的博客 273
上一篇: UVa455 Periodic Strings
下一篇: UVa1339 Ancient Cipher
DS_agent
博客等级 码龄11年 20粉丝 22原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值