封獣鵺 2022-02-24 14:47 采纳率: 100%
浏览 16
已结题

枚举法求一步万度最优解 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;
}
  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-02-24 15:26
    关注
    
    #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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月4日
  • 已采纳回答 2月24日
  • 创建了问题 2月24日

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)