UVA-Puzzle

https://cn.vjudge.net/contest/239026#problem/A

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

翻译:

30年前流行的儿童拼图由5×5帧组成,其中包含24个大小相等的小方格。每个小广场上都印有一个独特的字母表。由于框架内只有24个正方形,框架也包含一个与小正方形大小相同的空位置。一个正方形可以移动到那个空的位置,如果它立刻向右,向左,在上面,或者在空位置之下。谜题的目的是把正方形幻灯片放进空的位置,以便框架按字母顺序显示字母。下面的插图代表以下6个动作序列的原始配置及其配置中的一个难题:

1)空位上方的方块移动。

2)空位置的右边的方块移动。

3)空位置的右边的方块移动。

4)空位下方的正方形移动。

5)空位下方的正方形移动。

6)空位置左边的平方移动。

编写一个程序来显示得到的帧,并给出它们的初始配置和移动序列。

输入

输入程序包括几个难题。每一个都由它的初始配置和在谜题上的移动序列来描述。每个谜题描述的前5行是起始配置。随后的线条给出了一系列的动作。帧显示的第一行对应于拼图中的正方形的顶线。其他线路按顺序排列。帧中的空位置由空白指示。每个显示行包含5个字符,从最左边的字符开始(或者如果最左边的方框实际上是空的帧位置,则是空白)。显示线将对应于一个合法的谜题。移动序列由AS、BS、RS和LS的序列表示,表示哪个方块移动到空位置。A表示空位上方的正方形移动;B表示空位下方的正方形移动;L表示空位的左边移动的平方;R表示空位置的右边的方块移动。有可能存在非法移动,即使它由4个移动字符中的一个表示。如果发生非法移动,则认为该拼图没有最终配置。这一连串的动作可以分散在几行上,但总是以数字0结尾。数据的结尾用字符Z.表示。

产量

每一个谜题的输出都从一个适当的标记数字开始(谜题1,谜题2)等等。如果拼图没有最终的配置,那么应该遵循一个消息。否则,应该显示最终配置。

为最终配置格式化每行,以便在两个相邻字母之间有一个空白字符。对待空方块和字母一样。例如,如果空白是一个内部位置,那么它将出现为3个空白序列-一个将它从正方形向左分隔,一个用于空位置本身,一个将它从正方形向右分离。

通过一条空白行从不同的拼图记录中分离出输出。注:样本输入的第一个记录对应于上面所示的谜题。

样本输入

TrGSJ

XDOKI

M VLN

WPAB

UQHCF

ARBBL0

ABCDE

FGHIJ

KLMNO

PQRS

图瓦克斯

美国农业协会

LLL0

ABCDE

FGHIJ

KLMNO

PQRS

图瓦克斯

AAAAA BBRRRL0

Z

样本输出

谜题1:

T.G.S.J

X-K-Li

MD VBN

W PαA E

U-Q-HC-F

谜题2:

αA、B、C、D

F.H.I.E

KLM NJ

p q r s o

Tüv W x

谜题3:这个谜题没有最终的配置

#include <stdio.h>
#define MAXN 5
char grid[MAXN][MAXN];
int blankrow, blankcol, nextrow, nextcol;
int move(char move)
{
    if(move == 'A') {
        nextrow = blankrow - 1;
        nextcol = blankcol;
    } else if(move == 'B') {
        nextrow = blankrow + 1;
        nextcol = blankcol;
    } else if(move == 'L') {
        nextrow = blankrow;
        nextcol = blankcol - 1;
    } else if(move == 'R') {
        nextrow = blankrow;
        nextcol = blankcol + 1;
    } else
        return 1;
 
    if(nextrow >= 0 && nextrow < MAXN && nextcol >= 0 && nextcol < MAXN) {
        grid[blankrow][blankcol] = grid[nextrow][nextcol];
        grid[nextrow][nextcol] = ' ';
 
        blankrow = nextrow;
        blankcol = nextcol;
        return 0;
    } else
        return 1;
}
int main(void)
{
    int caseno=0, okflag, i, j;
    char c;
 
    while((c=getchar()) != 'Z') {
        // 读入数据
        for(i=0; i<MAXN; i++)
            for(j=0; j<MAXN; j++) {
                grid[i][j] = c;
                if(c == ' ' || c == '\n') {
                    blankrow = i;
                    blankcol = j;
                    grid[i][j] = ' ';
                }
                c = getchar();
                if(j == MAXN-1 && c == '\n')
                    c = getchar();
            }
        okflag = 1;
        while(c != '0') {
            if(c != '\n') {
                if(move(c)){
                    okflag = 0;
                    break;
                }
            }
            c = getchar();
            if(c == '\n')
                c = getchar();
        }
        while(c != '0')     // 跳过‘0’及以前的字符
            c = getchar();
        while(c != '\n')    // 跳过‘\n’及以前的字符
            c = getchar();
        if(++caseno > 1)
            printf("\n");
        printf("Puzzle #%d:\n", caseno);
        if(okflag) {
            for(i=0; i<MAXN; i++)
                    printf("%c %c %c %c %c\n", grid[i][0], grid[i][1], grid[i][2], grid[i][3], grid[i][4]);
        } else
            printf("This puzzle has no final configuration.\n");
    }
 
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值