枚举法求一步万度最优解 Segmentation Fault


#include<stdio.h>
static int complete = 0;
static int countSpin_max = 0;
static int stepMax = 0;
void plus(int* target ,int counted ,int base)
{
    int* lookfor = target+counted-1;
    *lookfor = *lookfor + 1;
    while(*lookfor >= base)
    {
        if(lookfor == target)
        {
            printf("完成\n");
            complete = 1;
            break;
        }
        else
        {
            int perm = *lookfor/base;
            *lookfor = *lookfor%base;
            lookfor = lookfor - 1;
            *lookfor = *lookfor + perm;
        }
    }//进位
}//枚举方案
void play(int* operate ,int* targetTable ,int countTable ,int* targetOrder ,int countOrder ,int width ,int height ,int* orderBest)
{
    int countSpin = 0;
    int k;
    for (k=0;k<countOrder;k++)
    {
        int over = 0;
        operate = targetTable + *(targetOrder + k);
        *operate = *operate + 1;
        countSpin++;
        while (over == 0)
        {
            if (*operate / 4 == 0) //0表示“上”
            {
                if (operate-targetTable>=width)
                {
                    operate = operate - width;
                    *operate = *operate + 1;
                    countSpin++;
                }
                else
                {
                    over = 1;
                }
            }
            else if (*operate /4 == 1) //1表示“右”
            {
                if ((operate-targetTable)%width<width-1)
                {
                    operate = operate + 1;
                    *operate = *operate + 1;
                    countSpin++;
                }
                else
                {
                    over = 1;
                }
            }
            else if (*operate /4 == 2) //2表示“下”
            {
                if ((operate-targetTable)/width<height-1)
                {
                    operate = operate + width;
                    *operate = *operate + 1;
                    countSpin++;
                }
                else
                {
                    over = 1;
                }
            }
            else if (*operate /4 == 3) //3表示“左”
            {
                if ((operate-targetTable)%width>0)
                {
                    operate = operate - 1;
                    *operate = *operate + 1;
                    countSpin++;
                }
                else
                {
                    over = 1;
                }
            }
        }
    }
    if (countSpin > countSpin_max)
    {
        countSpin_max =countSpin;
        for (int l=0;l<stepMax;l++)
        {
            *(orderBest+l)=*(targetOrder+l);
        }
            
    }
}
int main(void)
{
    int width;
    int height;
    printf("请输入宽度");
    scanf("%d",&width);
    printf("请输入高度");
    scanf("%d",&height);
    int table[width*height];
    int* operate;
    int i;
    printf("请输入限制步数");
    scanf("%d",&stepMax);
    int orderBest[stepMax];
    int order[stepMax];
    operate = &order[0];
    while(complete == 0)
    {
        operate = &table[0];
        for (i=0;i<(width*height);i++)
        {
            *operate = 0;
            operate++;
        }//初始化“游戏界面”
        plus(&order[0],stepMax,width*height);
        play(operate,&table[0],width*height,&order[0],stepMax,width,height,&orderBest[0]);
    }
    printf("最多可转%d个90度,方案为",stepMax);
    for (int m=0;m<stepMax;m++)
    {
        printf("%d,",orderBest[m]);
    }
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
static int complete = 0;
static int countSpin_max = 0;
static int stepMax = 0;
void plus(int* target ,int counted ,int base)
{
    int* lookfor = target+counted-1;
    *lookfor = *lookfor + 1;
    while(*lookfor >= base)
    {
        if(lookfor == target)
        {
            printf("完成\n");
            complete = 1;
            break;
        }
        else
        {
            int perm = *lookfor/base;
            *lookfor = *lookfor%base;
            lookfor = lookfor - 1;
            *lookfor = *lookfor + perm;
        }
    }//进位
}//枚举方案
void play(int* operate ,int* targetTable ,int countTable ,int* targetOrder ,int countOrder ,int width ,int height ,int* orderBest)
{
    int countSpin = 0;
    int k;
    for (k=0;k<countOrder;k++)
    {
        int over = 0;
        operate = targetTable + *(targetOrder + k);
        *operate = *operate + 1;
        countSpin++;
        while (over == 0)
        {
            if (*operate / 4 == 0) //0表示“上”
            {
                if (operate-targetTable>=width)
                {
                    operate = operate - width;
                    *operate = *operate + 1;
                    countSpin++;
                }
                else
                {
                    over = 1;
                }
            }
            else if (*operate /4 == 1) //1表示“右”
            {
                if ((operate-targetTable)%width<width-1)
                {
                    operate = operate + 1;
                    *operate = *operate + 1;
                    countSpin++;
                }
                else
                {
                    over = 1;
                }
            }
            else if (*operate /4 == 2) //2表示“下”
            {
                if ((operate-targetTable)/width<height-1)
                {
                    operate = operate + width;
                    *operate = *operate + 1;
                    countSpin++;
                }
                else
                {
                    over = 1;
                }
            }
            else if (*operate /4 == 3) //3表示“左”
            {
                if ((operate-targetTable)%width>0)
                {
                    operate = operate - 1;
                    *operate = *operate + 1;
                    countSpin++;
                }
                else
                {
                    over = 1;
                }
            }
        }
    }
    if (countSpin > countSpin_max)
    {
        countSpin_max =countSpin;
        for (int l=0;l<stepMax;l++)
        {
            *(orderBest+l)=*(targetOrder+l);
        }
            
    }
}
int main(void)
{
    int width;
    int height;
    printf("请输入宽度");
    scanf("%d",&width);
    printf("请输入高度");
    scanf("%d",&height);
    int *table;
    table=(int *)malloc(width*height*sizeof(int));
    if (table==NULL) {
        printf("分配%d字节内存失败!\n",width*height*sizeof(int));
        return 1;
    }
    int* operate;
    int i;
    printf("请输入限制步数");
    scanf("%d",&stepMax);
    int orderBest[stepMax];
    int order[stepMax];
    operate = &order[0];
    while(complete == 0)
    {
        operate = table;
        for (i=0;i<(width*height);i++)
        {
            *operate = 0;
            operate++;
        }//初始化“游戏界面”
        plus(&order[0],stepMax,width*height);
        play(operate,&table[0],width*height,&order[0],stepMax,width,height,&orderBest[0]);
    }
    printf("最多可转%d个90度,方案为",stepMax);
    for (int m=0;m<stepMax;m++)
    {
        printf("%d,",orderBest[m]);
    }
    free(table);
    return 0;
}