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, 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 #1Puzzle #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.

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 1000
using namespace std;

char s[N][N];

int main()
{
    int ca=1;

    while(gets(s[0]))
    {
       if (strcmp(s[0], "Z") == 0)
           return 0;

            gets(s[1]);
            gets(s[2]);
            gets(s[3]);
            gets(s[4]);

            int a,b;

        for(int i=0;i<5;i++)
        {
          for(int j=0;j<5;j++)
          {
             if(s[i][j]==' ')
             {
                 a=i;
                 b=j;
                 i=j=5;
             }
          }
        }

        bool f=false;
        bool tt=true;
        char str[N];

        while( !f && gets(str)  )//涨姿势了。判断条件的先后顺序决定了能否AC
        {
            for(int i=0;str[i]!=0;i++)
            {
                if(str[i]=='0' || !tt )
                {
                    f=true;
                    break;
                }

        switch(str[i]){
        case 'A':
        if(a==0)
         tt=false;

         else{
        s[a][b]=s[a-1][b];
        s[a-1][b]=' ';
        a--;
         }
         break;

    case 'B':
        if(a==4)
        tt=false;

        else{
        s[a][b]=s[a+1][b];
        s[a+1][b]=' ';
        a++;
        }
        break;

    case 'R':
        if(b==4)
        tt=false;

        else{
        s[a][b]=s[a][b+1];
        s[a][b+1]=' ';
        b++;
    }
    break;

    case 'L':
        if(b==0)
        tt=false;

        else{
        s[a][b]=s[a][b-1];
        s[a][b-1]=' ';
        b--;
        }
        break;
                }

            }
        }

        if(ca!=1)
        cout<<endl;

        cout<<"Puzzle #"<<ca++<<":"<<endl;
       if(tt)
       {
           for(int i=0;i<5;i++)
           {
             printf("%c %c %c %c %c\n",s[i][0],s[i][1],s[i][2],s[i][3],s[i][4]);
           }
       }
       else
       cout<<"This puzzle has no final configuration."<<endl;

    }

    return 0;
}

//自己写的,怎么改都过不了,看了网上别人的,又搞了一下午才搞懂。。。判断条件的顺序,第一次被这种东西坑。。。
//        while(gets(a))
//        {
//            tt=0;
//            int i=0;
//         while(a[i]!='0')
//         {
//
//            if(check(a[i],x,y)==0)
//            {
//                tt=1;
//            }
//               //cout<<"x="<<x<<"y="<<y<<" "<<"a[i]="<<a[i]<<" "<<"tt="<<tt<<endl;
//               i++;
//               if((a[i]!='A'&&a[i]!='B'&&a[i]!='L'&&a[i]!='R'))
//            {
//                 break;
//            }
//
//            if(tt || a[i]=='0')
//            {
//                f=1;
//                break;
//            }
//
//         }
         //cout<<f<<endl;
//
//         if(f || a[i]=='0')
//         break;
//
//        }
时间: 2024-12-11 12:08:50

uva227 谜题的相关文章

例题 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

谜题 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

Java谜题——库谜题

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

春节将至,又到了评绩效拿年终奖的时候!程序员绩效KPI 这个弥久历史谜题该怎么算呢?

关于程序员的绩效,像是一个弥久的历史谜题,长期困扰着大量的程序员与他们的领导们. KPI(Key Performance Indicators 关键绩效指标)是企业最爱用的绩效考核工具,但 KPI 通常只能定一些更宽泛的指标,且一般也只能分解到团队经理的头上,而很难分解到具体每个程序员的身上. 前不久看到个新闻,Amazon 美国的一个中国 IT 工程师在西雅图办公室跳楼自杀,原因是收到了 PIP.那 PIP 是什么?就是 Performance Improvement Plan 的简写,表达的

java解惑之字符之谜(谜题22)

谜题22:URL的愚弄 本谜题利用了一个java编程语言中一个鲜为人知的特性.请考虑下面的程序将会做什么? public class BrowerTest{ public static void main(String[] args){ System.out.ptintln("iexplore"); http://www.google.com; System.out.println(":maximize"); } } 这是个有点诡异的问题.当我们初次看到这个程序时,

【第二组】典型场景:用户上传自定义谜题,工作序号:002,2017/7/6

场景 工作项序号001:查看导入的图片,最后修改时间:2017/7/6 1. 背景 1) 典型用户:李二蛋[主要].龙傲天[次要] 2) 用户的需求/迫切需要解决的问题 李二蛋:看推理小说时间够长,我想自己写一个. 李二蛋:写了一个推理小说,我想去把它在生活中实现,让我的哥们玩. 李二蛋:我看着他们玩,在玩的时候当上帝. 龙傲天:我随手写了一个脚本,发送到系统,希望有人懂我,解开我设的谜题. 3) 假设: 用户上传谜题功能已实现 谜题转化成游戏的功能已实现 2. 场景 李二蛋登录了游戏,进入游戏

NOIP提高组 2016D1T1 玩具谜题

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=1563 题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外. 这时singer告诉小南一个谜題: “眼镜藏在我左数第3个玩具小人的右数第1个玩具小人的左数第2个玩具小人那里. ” 小南发现, 这个谜题中玩具小人的朝向非常关

noip 2016 玩具谜题

题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉小南一个谜題: “眼镜藏在我左数第3个玩具小人的右数第1个玩具小人的左数第2个玩具小人那里. ” 小南发现, 这个谜题中玩具小人的朝向非常关键, 因为朝内和朝外的玩具小人的左右方向是相反的: 面朝圈内的玩具小人, 它的左边是顺时针方向, 右边是逆时针方向; 而面向圈外的玩具小人, 它的左边是逆时针方

Python - 字母算术谜题

如: SEND + MORE == MONEY  9567  + 1085  == 10652 字母和数字一一对应 首字母不为0 解法: import re from itertools import permutations def solve(puzzle): puzzle= puzzle.upper() words = re.findall('[A-Z]+', puzzle) unique_characters = set(''.join(words)) assert len(unique