zoukankan      html  css  js  c++  java
  • UVa227

    227 Puzzle
    A children’s puzzle that was popular 30 years ago consisted of a 55 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
    Universidad de Valladolid OJ: 227 – Puzzle 2/3
    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
    Universidad de Valladolid OJ: 227 – Puzzle 3/3
    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.

    题意:

           给出一个5x5的表格,其中24格为字母,剩余一个是空的。给出一串操作序列:A表示将空格上面的格子向下滑动,B表示将空格下面的格子向上滑动,L表示将空格左面的格子向右滑动,R表示将空格右面的格子向左滑动。该操作序列一‘0’结尾。输出经过这些操作之后的字母表格(其中有一格是空的)。

    输入:

           多组数据,每组数据首先是5x5的字母表格,字母与字母之间没有空格,空的那一个格子以空格表示。之后的数行是操作序列,序列以‘0’结尾,中间可以有很多次换行。所有数据以‘Z’作为输出结束的标志。

    输出:

           每组数据输出其对应的字母表格,字母之间有空格。若存在不合法操作,则输出“不存在最终图像”。

    分析:

           模拟题。将操作序列的字母通过数组映射为横纵坐标的增量值,依次读取操作序列字符串中的字符,据此不断改变空白块的坐标,如果有一次移动使得空白块的坐标不合法则“没有最终的图像”。注意,scanf(“ %c”,&c)中的空格是为了忽略读取到的空白字符,这样做可以忽略操作字符串中的多处换行。

     1 #include <stdio.h>
     2 #include <string.h>
     3 const int LEN = 5;
     4 const int MAX = 100;
     5 const int dy[] = {0,0,1,-1};
     6 const int dx[] = {-1,1,0,0};
     7 char G[LEN][LEN];
     8 int tra[110];
     9 bool legal(int pos){return 0 <= pos && pos < LEN;}
    10 void Pmap(){
    11     for(int i = 0 ; i < LEN ; i++){
    12         printf("%c",G[i][0]);
    13         for(int j = 1 ; j < LEN ; j++)
    14             printf(" %c",G[i][j]);
    15         printf("
    ");
    16     }
    17 }
    18 int main(){
    19     tra['A'] = 0,tra['B'] = 1,tra['R'] = 2,tra['L'] = 3;
    20     bool first = true;
    21     int Case = 0,bx,by;
    22     while(gets(G[0])){
    23         if(G[0][0] == 'Z') break;
    24         for(int i = 1 ; i < LEN ; i++)
    25             gets(G[i]);
    26         for(int i = 0 ; i < LEN ; i++)
    27             for(int j = 0 ; j < LEN ; j++)
    28                 if(G[i][j]==' ') bx=i,by=j;
    29         bool ok = true;
    30         char c;
    31         while(scanf(" %c",&c),c != '0'){
    32             if(!ok) continue;
    33             int nx = bx + dx[tra[c]],ny = by + dy[tra[c]];
    34             if(!legal(nx) || !legal(ny)){
    35                 ok = false;
    36                 continue;
    37             }
    38             G[bx][by] = G[nx][ny];
    39             G[nx][ny] = ' ';
    40             bx = nx ; by = ny;
    41         }
    42         getchar();
    43         if(first) first = false;
    44         else printf("
    ");
    45         printf("Puzzle #%d:
    ",++Case);
    46         if(ok) Pmap();
    47         else printf("This puzzle has no final configuration.
    ");
    48     }
    49     return 0;
    50 }
    View Code
  • 相关阅读:
    R语言介绍
    mysql存储过程和函数的操作
    在SSRS自动化报表中创建共享数据源
    在python中实现数据库下group by功能
    MySQL中创建表及导入文件
    python下各种包的安装
    windows下python2.7.11的安装
    面向对象(异常)
    面向对象(内部类)
    面向对象(Object类)
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5769336.html
Copyright © 2011-2022 走看看