人工智能实验报告完整版八数码+验证 下载本文

内容发布更新时间 : 2024/5/19 0:06:46星期一 下面是文章的全部内容请认真阅读。

把数码问题就是把一串数字变成下面这个样子: 1 2 3 8 0 4 7 6 5 实现方法 1.

过程表示 源代码:

#include

static int style[9]={1,2,3,6,7,4,5,8,0};//输入的数码

//2,5,4,3,0,7,1,8,6 3,2,1,8,0,4,7,6,5 1,0,4,2,7,3,8,5,6 1,0,3,8,2,4,7,6,5

static int arrayStep41[6]={5,4,3,6,7,8};//第四步和第六步共用的数组,所以设为全局量 static int arrayStep71[4]={3,6,7,4}; static int local;//空格的位置 int i,j;//所用到的变量

int number=0;//记录移动步数 void print(); void step1(); void step2(); void step3(); void step4(); void step5(); void step6(); void step7(); void step8(); void step9();

void exchange(int x,int y); void judge();

void judge()//判断空格位置 { number = 0; for(i=0;i<9;i++) { if(style[i]==0) { local=i; return; } } }

void exchange(int x,int y)//交换两个数

{ int temp; print(); temp=style[x]; style[x]=style[y]; style[y]=temp; local=y; number++; }

void step1() { int arrayStep11[5]={3,0,1,2,5}; int arrayStep12[6]={6,7,8,5,2,1}; if((style[2]!=0)&&style[2]!=1) return; else { if(local==2) { if(style[1]==1) exchange(2,5); else exchange(2,1); return; } else { if(local==4) { exchange(4,1); i=2; while(local!=5) { exchange(arrayStep11[i],arrayStep11[i+1]); i++; } return; } for(i=0;i<3;i++) { if(arrayStep11[i]==local) { while(local!=5) {

exchange(arrayStep11[i],arrayStep11[i+1]); i++; } return; } } for(i=0;i<4;i++) { if(arrayStep12[i]==local) { while(local!=1) { exchange(arrayStep12[i],arrayStep12[i+1]); i++; } return; } } } } return; }

void step2() { int arrayStep21[8]={0,3,6,7,8,5,4,1}; for(i=0;i<8;i++) { if(arrayStep21[i]==local) { while(style[0]!=1) { exchange(arrayStep21[i%8],arrayStep21[(i+1)%8]); i++; } break; } } } void step3() { int arrayStep31[8]={2,1,4,3,6,7,8,5}; for(i=0;i<8;i++) { if(arrayStep31[i]==local)