UVa227 - Puzzle

题目

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.

注:输出中使用了*来代替目标空格。

我的解读

读入

首先可以看见这题没有像往常一样给出题目数量N,而是通过特定字符('Z')来结束。这个问题很好解决。只需每次读入数据时判断第一个输入字符是不是'Z'就能解决。更具体一点,可以在一个循环的条件先读入第一行字符串,然后进行判断,若符合则跳出循环即可结束程序,否则继续运行。(当然,亦可以先用getchar读入第一个字符,然后判断)

然后是读入的字符含有空格的问题。这意味着不可以使用scanf来进行读入,只能使用fgets来读入一行 或者 getchar来一个一个读入。(但getchar会读入 \n 所以需要稍作处理)

之后需要读入的是操作指令,结束符同样进行了替换('0'),同时操作指令中可能含有 \n 。就是说只读入字符串的做法行不通,需要特殊处理。(笔者的做法是用getchar每读入一个字符进行一次操作,但需要处理命令结束后的 \n)

输出

输出的要求也比往常苛刻,但是不难处理。
第一个是输出当前题号,只需用一个变量计数即可。
然后输出答案只需每一行的第一个字符或者最后一字符不输出空格即可。
之后是每一题之间都要有一行间隔,但是最后一题时不能有空白行。也比较容易实现。

核心考点

输入函数的灵活运用,输出格式的掌握。

代码

#include <stdio.h>
#include <string.h>
#define MAXN 10000

int main() {

    char map[5][7];
    int kase = 0;
    while (fgets(map[0], 7, stdin)) {
        if (map[0][0] == 'Z')
            break;
        for (int i = 1; i < 5; i++)
            fgets(map[i], 7, stdin);

        int x, y;
        for (int i = 0; i < 5; i++)
            for (int j = 0; j < 5; j++)
                if (map[i][j] == ' ') {
                    x = i;
                    y = j;
                }

        int x1 = x, y1 = y;
        bool flag = true;
        char order;
        while ((order = getchar()) != '0') {
            switch (order) {
            case 'A':
                x1--; break;
            case 'B':
                x1++; break;
            case 'L':
                y1--; break;
            case 'R':
                y1++; break;
            default:
                break;
            }
            if (x1 > 4 || x1 < 0 || y1>4 || y1 < 0)
                flag = false;
            else {
                map[x][y] = map[x1][y1];
                map[x1][y1] = ' ';
                x = x1; y = y1;
            }
        }
        getchar();

        if (kase++) printf("\n");
        printf("Puzzle #%d:\n", kase);
        if (!flag)
            printf("This puzzle has no final configuration.\n");
        else {
            for (int i = 0; i < 5; ++i) {
                printf("%c", map[i][0]);
                for (int j = 1; j < 5; ++j)
                    printf(" %c", map[i][j]);
                printf("\n");
            }
        }

    }

    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容