UVa227

这一题是就是简单的模拟题。用一个二维数组去存,然后进行模拟。比较烦人的地方是对于数据的读取和处理。今天一整个下午因为这一题的玄学问题花式WA,最后网上找了AC代码,发现思路基本相同,(样例我也过了)反正我的代码就是WA,网上找的代码就是AC(#懵逼)


#include <stdio.h>
#include <string.h>

int main () {
    int cases = 0;
    bool line = false;
    char initial [5] [7];

    while ( gets (initial [0]) ) {

        if ( strcmp (initial [0], "Z") == 0 )
            return 0;

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

        int blank_x;
        int blank_y;

        for ( int i = 0; i < 5; i++ ) {
            for ( int j = 0; j < 5; j++ ) {
                if ( initial [i] [j] == ' ' ) {
                    blank_x = i;
                    blank_y = j;
                    i = j = 5;
                }
            }
        }

        char command [1000];
        bool valid = true;
        bool exit_koro = false;

        while ( !exit_koro && gets (command)) {

            for ( int i = 0; command [i] != 0; i++ ) {

                if ( command [i] == '0' || !valid ) {
                    exit_koro = true;
                    break;
                }

                switch (command [i]) {
                case 'A' :
                    if ( blank_x == 0 )
                        valid = false;
                    else {
                        initial [blank_x] [blank_y] = initial [blank_x - 1] [blank_y];
                        initial [blank_x - 1] [blank_y] = ' ';
                        blank_x--;
                    }
                    break;

                case 'B' :
                    if ( blank_x == 4 )
                        valid = false;
                    else {
                        initial [blank_x] [blank_y] = initial [blank_x + 1] [blank_y];
                        initial [blank_x + 1] [blank_y] = ' ';
                        blank_x++;
                    }
                    break;

                case 'R' :
                    if ( blank_y == 4 )
                        valid = false;
                    else {
                        initial [blank_x] [blank_y] = initial [blank_x] [blank_y + 1];
                        initial [blank_x] [blank_y + 1] = ' ';
                        blank_y++;
                    }
                    break;

                case 'L' :
                    if ( blank_y == 0 )
                        valid = false;
                    else {
                        initial [blank_x] [blank_y] = initial [blank_x] [blank_y - 1];
                        initial [blank_x] [blank_y - 1] = ' ';
                        blank_y--;
                    }
                    break;
                }
            }
        }

        if ( line )
            printf ("\n");
        line = true;

        printf ("Puzzle #%d:\n", ++cases);

        if ( valid ) {
            for ( int i = 0; i < 5; i++ ) {
                printf ("%c %c %c %c %c\n", initial [i] [0], initial [i] [1],
                        initial [i] [2], initial [i] [3], initial [i] [4]);
            }
        }

        else
            printf ("This puzzle has no final configuration.\n");

    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值