首页 > 其他分享 >谜题(Puzzle)

谜题(Puzzle)

时间:2022-11-28 18:31:22浏览次数:49  
标签:square Puzzle 谜题 position moves blankPosY blankPosX empty


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 letter of thealphabet was printed on each small square. Since there were only 24 squareswithin the frame, the framealso 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 tothe right, to the left, above, or below the empty position. The object of thepuzzle was to slide squares into the empty position so that the framedisplayed the letters in alphabetical order.

The illustration below represents a puzzle in its original configuration andin 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 configurationsand sequences of moves.

Input

Input for your program consists of several puzzles. Each is described by itsinitial configuration and the sequence of moves on the puzzle. The first 5lines of each puzzle description are the starting configuration. Subsequentlines give the sequence of moves.

The first line of the frame display corresponds to the top line of squaresin the puzzle. The other lines follow in order. The empty position in a frameis indicated by a blank. Each display line contains exactly 5 characters,beginning with the character on the leftmost square (or a blank ifthe leftmost square is actually the empty frame position). The display lineswill correspond to a legitimate puzzle.

The sequence of moves is represented by a sequence of As, Bs, Rs, and Ls todenote which square moves into the empty position. A denotes that the squareabove the empty position moves; B denotes that the square below the emptyposition moves; L denotes that the square to the left of the emptyposition moves; R denotes that the square to the right of the empty positionmoves. It is possible that there is an illegal move, even when it isrepresented by one of the 4 move characters. If an illegal move occurs, thepuzzle is considered to have no final configuration. This sequence of movesmay be spread over several lines, but it always ends in the digit 0. Theend 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, thena message to that effect should follow. Otherwise that final configurationshould be displayed.

Format each line for a final configuration so that there is a single blankcharacter between two adjacent letters. Treat the empty square the sameas a letter. For example, if the blank is an interior position, then it willappear as a sequence of 3blanks - one to separate it from the square to the left, one for theempty 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 的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A,B,L,R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指令序列(以数字 0 结束),输出指令执行完毕后的网格。如果有非法指令,应输出“This puzzle has no final configuration."。

【分析】

       (分析过程附加在程序注释中)

用java语言编写程序,代码如下:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char[][] in = new char[5][5];//存储网格字符
char[] arr;//指令序列
//空格的位置 (blankPosX, blankPosY)
int blankPosX = 0, blankPosY = 0, x, y;
int puzzleCase = 0;//谜题的序号
boolean first = true;//用于判断是否为第一谜题答案的输出,如果是则不能输出换行,否则输出换行
while(input.hasNext()) {
String str;
//读取换行(为什么这样做的原因看下面注释)
if(puzzleCase != 0)
str = input.nextLine();

boolean ans = true;

//初始化网格
str = input.nextLine();
if(str.equals("Z"))
break;

for(int i = 0; i < 5; i++) {
in[0][i] = str.charAt(i);
if(in[0][i] == ' ') {
blankPosX = 0;
blankPosY = i;
}
}

for(int i = 1; i < 5; i++) {
str = input.nextLine();
for(int j = 0; j < 5; j++) {
in[i][j] = str.charAt(j);
if(in[i][j] == ' ') {
blankPosX = i;
blankPosY = j;
}
}
}

//读取指令序列
boolean isEnd = false;
String temp = "";
while(!isEnd) {
//由于此处使用next()进行读取,当我们输入指令后进行换行时,换行符未被读取
//当重新开始新谜题的网格读取时,使用nextLine()方法,它将会读取换行
//这也是为什么在程序一开始要先用nextLine()方法读取一下的原因。
//当然第一个谜题时,我们并不需要读取换行。
temp = temp + input.next();
if(temp.charAt(temp.length() - 1) == '0')
isEnd = true;
}

arr = temp.toCharArray();//将字符串指令转化为字符数组

for(int i = 0; i < arr.length; i++) {
if(arr[i] == '0')
break;

//非法指令:数组越界。我们只要先判断当发生某种交换时是否将发生数组越界,就可以知道该指令是否非法
if(arr[i] == 'A') {
if(blankPosX == 0) {
ans = false;
break;
}
else {
swap(in, blankPosX, blankPosY, blankPosX - 1, blankPosY);
blankPosX = blankPosX - 1;
}
}
else if(arr[i] == 'B') {
if(blankPosX == 4) {
ans = false;
break;
}
else {
swap(in, blankPosX, blankPosY, blankPosX + 1, blankPosY);
blankPosX = blankPosX + 1;
}
}
else if(arr[i] == 'L') {
if(blankPosY == 0) {
ans = false;
break;
}
else {
swap(in, blankPosX, blankPosY, blankPosX, blankPosY - 1);
blankPosY = blankPosY - 1;
}
}
else if(arr[i] == 'R'){
if(blankPosY == 4) {
ans = false;
break;
}
else {
swap(in, blankPosX, blankPosY, blankPosX, blankPosY + 1);
blankPosY = blankPosY + 1;
}
}
}

if(first)
first = false;
else
System.out.println();

System.out.println("Puzzle #" + (++puzzleCase) + ":");

if(ans == false)
System.out.println("This puzzle has no final configuration.");
else {
for(int i = 0; i < 5; i++) {
System.out.print(in[i][0]);
for(int j = 1; j < 5; j++)
System.out.print(" " + in[i][j]);
System.out.println();
}
}
}
}

//交换字符的位置
public static void swap(char[][] arr, int x1, int y1, int x2, int y2) {
char temp = arr[x1][y1];
arr[x1][y1] = arr[x2][y2];
arr[x2][y2] = temp;
}
}



标签:square,Puzzle,谜题,position,moves,blankPosY,blankPosX,empty
From: https://blog.51cto.com/u_15894233/5893381

相关文章

  • P3869 机关谜题
    从今以后,我将会用一个假的标题!当然,不是每天都是愚人节(传送门???思路状压+爆搜。状压——压机关的触发。比如:11010B就表示第2、第4、第5个机关被触发了奇数次。......
  • 玩具谜题(NOIP2016)
    题目链接:​​玩具谜题​​​提高组日常水题。直接模拟,有需要注意的点会在代码后讲解:#include<bits/stdc++.h>usingnamespacestd;intmain(){intn,m;scanf("%......
  • 谜题:打造极小ELF文件输出文件(使用汇编语言通过系统调用来实现)
    接上文《谜题:打造极小ELF文件输出文件(通过C语言来实现)》在本篇中,我们要写出一段直接通过系统调用的方式、且使用尽可能少的指令的汇编代码来实现目标。可以省略的代码,就......
  • 谜题:打造极小ELF文件输出文件(在Linux环境中精简ELF64文件)
    接前文《谜题:打造极小ELF文件输出文件(使用汇编语言通过系统调用来实现)》在完成了一个232字节的程序后,发现距离186字节的目标还是有一些距离。接下来就要深入研究ELF文件的......
  • A Number Puzzle
    题目:题解:全排列函数#include<bits/stdc++.h>usingnamespacestd;inta[15];intans[10000005];intmain(){intn,m;while(scanf("%d%d",&n,&m)!=EOF){......
  • CCPC 2022桂林 J.Permutation Puzzle
    模拟赛的时候这道题细节写挂了,硬是调不出来。。。首先想到拓补排序。然后可以发现,正反各跑一次可以获得每个点的取值范围,即上界和下界。(特殊地,对于已知点,其上下界相等且等......
  • CF914D Bash and a Tough Math Puzzle
    题目链接:​​传送门​​问一个区间修改一个数能不能让这个区间gcd变为x;带单点修改直接维护每个区间gcd递归到叶子节点且%x不为0时cnt++如果cnt>1就不行半年前的代码了#i......
  • puzzle(103.1)网格图一笔画
    目录​​一,一笔画完(网格图带起点)​​​​二,网格图不带起点​​​​三,六边形网格图​​一,一笔画完(网格图带起点)一笔画完(微信小程序游戏):这个游戏和我攻略过的另外2个游戏相关......
  • CF627F Island Puzzle.
    容易观察到一次操作是将\(0\)移动到一个相邻的节点上,而且操作可逆转。所以对于不加边的情况方案是唯一的,直接模拟一下看看是不是相等的就好。对于加一条边来说,我们先将......
  • vue-puzzle-vcode与vue-drag-verify纯前端的拼图人机验证、右滑拼图验证
    转载作品!以获取原作者允许,原文地址,感觉写的比较全面,也比较实用,大家可以去看看原文章;纯前端的拼图人机验证、右滑拼图验证1、vue-puzzle-vcodegithub地址:https://github......