Puzzle UVa227(分析和问题解决)

 一想到这个题目AC的情景啊,那个激动兴奋的,交了几十遍。。。。各种错误,真的是说不出来的感觉。但是收获也不少。在此分享。

  1. 从技术上来说,调用指针和使用函数封装功能能加快代码的运行速度和简化代码。
  2. 出现Runtime Error的时候首先最最最重要的是检查一个地方。第一,所有带循环的地方,一定要看有没有死循环的。第二,数组有没有越界,特别是使用指针的时候,这种情况很容易发生,但是非常难查出来。所以提前就要把范围看好了,并检查,这样会省很多时间走弯路,因为你很难想到哪个指针指偏了(就像这里的代码,我最早的时候写的是+25,怎么检查都想不明白哪儿有问题,等最后才想到这个,都已经浪费了一下午了。)
  3. 对于格式,一定要与题目一致,就比如这题竟然最后一个输出后面不能有空行(心塞)。还有有点地方需要使用getchar()吃掉回车,防止被放在Puzzle里面那就。。。。
  4. 对于检查,我们可以重载printf(),使他可以对文件流进行操作,极大的方便看我们的输出。当然输入也方便许多。
  5. 要是实在想不明白,解决不了的地方,一个可以逛逛别的人的提问和源码,一个也可以选择走一走散散心,回来再想,这样也不至于困苦到砸键盘是吧。
  6. 努力总是有回报的。

 

 

 

code:

#include<stdio.h>
#include<string.h>
//#define LOCAL

char puzzle[35];


// the function to change position.
// A is the movement.
// B is the array of Puzzle.
int chposition(char a, char b[])
{
    char c('0');
    char* temp = strchr(b, ' ');
    // return 1 when occurs the wrong move or out of the range.
    if( a == 'A')
    {    if(temp-5 < b) return 1; c = *(temp-5); *(temp-5) = *temp; *temp = c; temp = temp - 5;}
    else if( a == 'B')
    {    if(temp+5 > b+24) return 1;    c = *(temp+5); *(temp+5) = *temp; *temp = c; temp = temp + 5; }
    else if( a == 'L')
    {    if((temp-b)%5-1 < 0) return 1; c = *(temp-1); *(temp-1) = *temp; *temp = c; temp = temp - 1; }
    else if( a == 'R')
    {    if((temp-b)%5+1 > 4 ) return 1; c = *(temp+1); *(temp+1) = *temp; *temp = c; temp = temp + 1;}
    else return 1;
    return 0;
    
}

int main()
{
    //overload the printf and scanf.
    #ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
    #endif
    int count = 0;
    int first = 1;
    while(true)
    {
        memset(puzzle,0,35);
        count++;
        char c = getchar();
        //if ( c == '\n') c = getchar();    //ignore the '\n'
        if( c == 'Z') break;              //'Z' the signal of the end.
        else puzzle[0] = c;
        int i = 1;
        for(; i < 25; i++)
        {
            puzzle[i] = getchar();
            if(puzzle[i] == '\n') i--;
        }
        int flag = 0;
        int taq(0);
        while( (c = getchar()) && c != '0')
        {
            if(c == '\n')continue;
            flag = chposition(c, puzzle);
            if(flag == 1)  taq = 1; 
        }
        getchar();
        if( first == 1) {
            first = 0;
        }
        else{
            putchar('\n');
        }    
        printf("Puzzle #%d:\n",count);
        if(taq) 
            printf("This puzzle has no final configuration.\n");
        else
        {
            for(i = 0; i <25; i++)
            {
                if((i+1)%5 == 0)printf("%c\n", puzzle[i]);
                else
                {
                    printf("%c ", puzzle[i]);
                }
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/dreamworldclark/p/9420319.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值