例题 3-5 谜题 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. Since there
were only 24 squares within the frame, the frame also contained an empty position which was the same
size as a small square. A square could be moved into that empty position if it were immediately to the
right, to the left, above, or below the empty position. The object of the puzzle was to slide squares
into 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 after
the 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 and
the sequence of moves on the puzzle. The first 5 lines of each puzzle description are the starting
configuration. 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 other
lines follow in order. The empty position in a frame is indicated by a blank. Each display line contains
exactly 5 characters, beginning with the character on the leftmost square (or a blank if the leftmost
square 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 square
moves into the empty position. A denotes that the square above the empty position moves; B denotes
that the square below the empty position moves; L denotes that the square to the left of the empty
position moves; R denotes that the square to the right of the empty position moves. It is possible that
there is an illegal move, even when it is represented by one of the 4 move characters. If an illegal move
occurs, the puzzle is considered to have no final configuration. This sequence of moves may be spread
over 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.). If
the puzzle has no final configuration, then a message to that effect should follow. Otherwise that final
configuration should be displayed.
Format each line for a final configuration so that there is a single blank character between two
adjacent letters. Treat the empty square 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 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.

有  一个4*4的网格,其中恰好有一个 格子是空的 其他的 格子  各有一个字母 现在 给你四种操作    也就是   A,B,L,R分别表示把空格上下左右的相邻字母 移动到 空格内    现在  开始输入    输入 : 前四行 是 网格 之中的字母    然后紧接着的是   一系列操作

如果  输入不正确的话   就输出

This puzzle has no final configuration.
时间: 2024-12-14 09:44:35

例题 3-5 谜题 uva227的相关文章

谜题 UVA227

这道题目还是不难的,但是要注意gcc里面gets已经不能用了,用gets_s还是可以的,尽管我并不知道有什么区别 #include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char a[20][20]; int x, y; char s[1000]; int blank_x = 0, blank_y = 0; int flag = 1; gets_s(a[0]); gets_s(a[1]); get

算法竞赛入门经典第二版第三章习题

写这个的原因是看到一位大神的习题答案总结,于是自己心血来潮也想写一个这个,目的主要是督促自己刷题吧,毕竟自己太弱了. 习题3-1 得分 UVa 1585 大致就是设置一个变量记录到当前为止的连续的O的数量,碰到X就变0,水题. #include<stdio.h> #include<ctype.h> #include<string.h> char s[90]; int main(void) { int length,n,sum,num; scanf("%d&qu

uva227 谜题

 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 square. Since there were only 24 squares within the frame, t

例题1.19 计算器谜题 UVa11549

1.题目描述:点击打开链接 2.解题思路:本题是一道普通的模拟题,根据题意易知计算器显示的数将会出现循环,因为显示的n位数一共就有限种情况,而平方的次数是无限多的.所以不妨一个个的模拟,每次都看新得到的数是否以前出现过,如果出现过就跳出循环.但如何判断是否出现过呢?第一种方法是利用STL中的set,同时写一个Next函数求出下一个k值.下面一共给出三个不同的代码,来分别体会一下效率上的巨大差异. 3.代码: (一,Next函数中使用string,stringstream) #define _CR

Java谜题——库谜题

1.Java中的不可变对象和可变对象 (1)不可变类:当你获得这个类的实例的引用之后,你不可以改变这个实例的内容.比如:String,BigInteger,BigDecimal,还有基本数据类型的封装类,这些都是不可变类.用实例来调用方法时,不会改变里面的变量的值.代码: import java.math.BigInteger; public class BigProblem { public static void main(String[ ] args) { BigInteger fiveT

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法

黑书例题 Fight Club 区间DP

题目可以在bnuoj.soj等OJ上找到. 题意: 不超过40个人站成一圈,只能和两边的人对战.给出任意两人对战的输赢,对于每一个人,输出是否可能是最后的胜者. 分析: 首先序列扩展成2倍,破环成链. dp[i][j]表示i和j能够相遇对打,那么dp[i][i+n]为真代表可以成为最后胜者. 枚举中间的k,若i和j都能和k相遇,且i和j至少一人能打赢k,那么i和j可以相遇. 复杂度o(n^3) 1 #include<cstdio> 2 #include<cstring> 3 usi

linux脚本进阶例题解析

例题一:编写脚本/root/bin/createuser.sh,实现如下功能: 使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之:并生成8位随机口令并存在一个文件中,初步提示改口令,显示添加的用户的id号等信息 #!/bin/bash # ------------------------------------------ # Filename: useradd.sh  # Revision: null # Date: 2017-09-11 21:47:22 # Auth

JAVA基础汇总及例题详解

java语言的一个核心: jdk, java development kits---面向开发人员 jre, java Runtime Environment---服务器上 java虚拟机---(以字节码为指令的CPU)---*.class java编译器-->字节码-->类加载器进行验证-->虚拟机运行 垃圾回收机制 public class ...{ int a = 1; } c/c++垃圾回收由程序员去运行 java编程语言(一门纯面向对象)的特点: 1, 面向对象  封装  继承