本小白最近刚刚接触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];elsecout<<" "<<m[i][j];}cout<<endl;}}}return 0;
}

UVA-227-Puzzle相关推荐

  1. UVA - 227 Puzzle

    Puzzle UVA - 227 题目传送门 注意点:每两个输出点间有一个换行,但最后一个输出无换行 恶心模拟题,很卡输入输出!!! AC代码1:(自己的代码,提交时需要选择C++11) #inclu ...

  2. 【UVA - 227】Puzzle (模拟,水题)

    题干: Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contain ...

  3. 227 Puzzle

    这个题是给出一个5*5的方格 里面有一个空格,要求输入各种字符来代表空格的移动并输出移动后的5*5图形, ***这个题给我的收获是如何跳出一个多重循环,比如for(x=0;x<5;x++)for ...

  4. π-Algorithmist分类题目(1)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(1) Sorting UVAL ...

  5. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  6. WaWa的奇妙冒险(第一周集训自闭现场)

    第一周周记 (一)例题记录 A-Download Manager (水题) HDU - 3233 Input Output Sample Input Sample Output 理解 AC代码 B-J ...

  7. 习题3-1至习题3-5

    注:题目来自刘汝佳的<算法竞赛入门经典第2版>,在vjudge选择OJ平台UVA进行提交.必要的时候会写解题思路,简单的题纯粹就做个记录. 3-1 UVA1585 #include< ...

  8. 汉字对应的ASCLL

    printf("%c%c*%c%c",206,196,208,249);输出的是 文*轩 . 以下代码有bug,仅供理解 #include <stdio.h> #inc ...

  9. 紫书搜索 习题7-8 UVA - 12107 Digit Puzzle IDA*迭代加深搜索

    题目链接: https://vjudge.net/problem/UVA-12107 题意: 给出一个数字谜,要求修改尽量少的数,使修改后的数字谜只有唯一解.空格和数字可以随意替换,但不能增删,数字谜 ...

  10. Puzzle (II) UVA - 519

    题目链接: https://vjudge.net/problem/UVA-519 思路: 剪枝+回溯 这个题巧妙的是他按照表格的位置开始搜索,也就是说表格是定的,他不断用已有的图片从(0,0)开始拼到 ...

最新文章

  1. python使用matplotlib可视化线图(line plot)、在可视化图像中的指定位置添加横线(add horizontal line in matplotlib plot)
  2. 链表是否带环、环入口、环长度、链表相交问题分析与总结
  3. (十)IDEA添加mybatis-mapp.xml文件
  4. 判读一个对象不为空_ArrayList实现分析(一)——对象创建
  5. python functools模块方法
  6. CCF NOI1000 加密算法
  7. Mac解压Windows 压缩文件(.zip格式)乱码问题
  8. Node后端数据渲染
  9. 【渝粤教育】21秋期末考试市场营销10256k2
  10. 基于H5的移动端APP开发框架
  11. 安装LuaRocks
  12. 将Android Studio的设置恢复到初始化(清除所有的设置)
  13. 【原创分享·支付宝支付】HBuilder打包APP调用支付宝客户端支付
  14. vue+animation实现翻页动画
  15. 某机主存容量为4Nx16位,且存储字长等于指令字长,若该机指令系统可完成108种操作,操作码位数固定,且具有直接、间接、变址、基址、相对、立即等六种寻址方式
  16. matlab复数fft,第30章 复数FFT的实现
  17. 思考14. 为什么我们要全力以赴
  18. 无法打开模块文件“C:\Users\sq\AppData\Local\Temp\.NETFramework,Version=v4.5,AssemblyAttributes.vb”:系统找不到指定的文件
  19. 关于启动ubuntu虚拟机时遇到Host SMBus controller not enabled的解决办法
  20. 退休后的程序员,不会遇到太多经济困难?

热门文章

  1. 1070: 小汽车的位置
  2. 机器学习之大规模数据处理
  3. Symbian 编程(二)——运行你的第一个sis程序(转)
  4. 这个时代,到处都是才华横溢的穷光蛋
  5. pip安装gunicorn_gunicorn
  6. #HPDC 华为伙伴暨开发者大会2022随笔
  7. 听呆了,看到了《诗经》中的古风!
  8. SpringMVC教程(二)
  9. vue-cli跨域相关
  10. 加载datalodaer 调用 __getitem__方法验证