[UVA227]Puzzle

注意事项:
1.这道题主要难度在于处理输入输出和审题。我从中的体会在于要充分的思考到各种情况的处理方式。
2.在输入输出方面要严格的注意空格和空行,特别是最后不需要空行。另外要注意的一点是,吃掉空行和错误的命令后面的命令(我就是因为这个没有注意而且有些急躁推倒重来好多次)。
3.审题方面的问题在于,谜题的输入是比较规范的,但是移动命令的输入很不规范,会出现非法的命令。特别的有两种情况, 一是越界, 二是ABLR之外的命令。还有就是命令不一定在一行当中出现。
4.运用scanf(” %c”, &xxx)可以处理不连续的命令。
最后附上代码。

#include <stdio.h>

int main(void)
{
    int var[128][2] = { 0 };
    char puzzle[5][6];
    int temp_row, temp_column, count_puzzle, count_get, var_row, var_column, cal_row, cal_column;
    char temp_order;

    count_puzzle = 0;
    var['A'][0] = -1;
    var['B'][0] = 1;
    var['R'][1] = 1;
    var['L'][1] = -1;

    while (gets(puzzle[0]))
    {
        if (puzzle[0][0] == 'Z')
            break;

        if (count_puzzle++ != 0)
            printf("\n");
        printf("Puzzle #%d:\n", count_puzzle);

        for (count_get = 1; count_get < 5; count_get++)
            gets(puzzle[count_get]);

        for (temp_row = 0; temp_row < 5; temp_row++)
            for (temp_column = 0; temp_column < 5; temp_column++)
                if (puzzle[temp_row][temp_column] == 32)
                    goto _break_one;
    _break_one:
        while (1)
        {
            scanf(" %c", &temp_order);
            if (temp_order == '0')
                break;
            var_row = var[temp_order][0];
            var_column = var[temp_order][1];
            if (var_column == 0 && var_row == 0)
                goto _break_error;
            cal_row = temp_row + var_row;
            cal_column = temp_column + var_column;
            if (cal_row > 4 || cal_row < 0 || cal_column < 0 || cal_column > 4)
                goto _break_error;
            puzzle[temp_row][temp_column] = puzzle[cal_row][cal_column];
            temp_row = cal_row;
            temp_column = cal_column;
        }

        puzzle[temp_row][temp_column] = 32;
        goto _break_correctness;

    _break_error:
        printf("This puzzle has no final configuration.\n");
        while (getchar() != '0');
        while (getchar() != '\n');
        continue;
    _break_correctness:
        for (temp_row = 0; temp_row < 5; temp_row++)
        {
            for (temp_column = 0; temp_column < 4; temp_column++)
                printf("%c ", puzzle[temp_row][temp_column]);
            printf("%c\n", puzzle[temp_row][4]);
        }

        while (getchar() != '\n');
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值