#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;
}
枚举法求一步万度最优解 Segmentation Fault
- 写回答
- 好问题 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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
- ¥20 @microsoft/fetch-event-source 流式响应问题
- ¥15 ogg dd trandata 报错
- ¥15 高缺失率数据如何选择填充方式
- ¥50 potsgresql15备份问题
- ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
- ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
- ¥60 pb数据库修改与连接
- ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
- ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?