欢迎大家光临【无师自通-教程网】您的到来是我们的荣幸。本站提供photoshop教程,ps教程,flash教程,cad教程,网页制作教程,excel教程,asp教程,vb教程,3d教程,c语言教程,html教程,coreldraw教程,dreamweaver教程,java教程,3dmax教程 等各种教程为主题的内容和服务,相信您会在这里找到您所需要的东东。无师自通伴您一生-谢谢您的光临!!
网站地图 设为首页
简繁切换 加入收藏
栏目待定 留言本站
您现在的位置: 无师自通-教程网 >> 程序设计 >> VC语言 >> VC语言 >> 教程正文

  没有公告

教程: 拼图游戏的算法 更多...
教程: 拼图游戏的算法

int x,y;
}path[1000];
int zx[4]={-1,0,1,0};
int zy[4]={0,-1,0,1};
int top;
int desti[4][4];//目标状态
int detect(struct node *p)
{int i,j;
for(i=1;i<4;i )
for(j=1;j<4;j )
if(p->nodesun[i][j]!=desti[i][j])
return 0;
return 1;
}

void printlj()
{int tempt;
int i,j;
tempt=1;
while(tempt<=top)
{ for(i=1;i<4;i )
for(j=1;j<4;j )
{cout<<path[tempt].nodesun[i][j];
if(j==3)
cout<<" "<<endl;
}
tempt ;
}
}




void main()
{ //初始化
int i,j,m,n,f;
int temp,find=0;
top=1;
for(i=1;i<4;i )
for(j=1;j<4;j )
{cout<<"请输入第"<<i<<"行"<<"第"<<j<<"列的值"<<endl;
cin>>temp;
path[1].nodesun[i][j]=temp;
}
cout<<"请输入初始状态的空格的位置(行)"<<endl;
cin>>temp;
path[1].x=temp;
cout<<"请输入初始状态的空格的位置(列)"<<endl;
cin>>temp;
path[1].y=temp;
//目标状态
for(i=1;i<4;i )
for(j=1;j<4;j )
{cout<<"请输入目标状态第"<<i<<"行"<<"第"<<j<<"列的值"<<endl;
cin>>temp;
desti[i][j]=temp;
}
//深度优先搜索
while(!find)
{ m=path[top].x;
n=path[top].y ;
for(f=0;f<4;f )
{i=m zx[f];
j=n zy[f];
if(i>=1&&i<=3&&j>=1&&j<=3)
{top ;
path[top]=path[top-1];
path[top].nodesun[m][n]=path[top-1].nodesun[i][j];
path[top].nodesun[i][j]=0;

path[top].x=i;
path[top].y=j;

if(detect(&path[top]))
{printlj();
find=1;
break;
}

break;
}//if

}//for

}//while
}



#include"iostream.h"
struct node{
int nodesun[4][4];
int pre;
int x,y;
}path[400];
int zx[4]={-1,0,1,0};
int zy[4]={0,-1,0,1};
int front,rear;
int desti[4][4];//目标状态
int detect(struct node *p)
{int i,j;
for(i=1;i<4;i )
for(j=1;j<4;j )
if(p->nodesun[i][j]!=desti[i][j])
return 0;
return 1;
}

void printlj()
{int tempt;
int i,j;
tempt=rear;
while(tempt!=0)
{ for(i=1;i<4;i )
for(j=1;j<4;j )
{cout<<path[tempt].nodesun[i][j];
if(j==3)
cout<<" "<<endl;
}
tempt=path[tempt].pre;
}
}




void main()
{ //初始化
int i,j,m,n,f;
int temp,find=0;
front=rear=1;
path[1].pre=0;
for(i=1;i<4;i )
for(j=1;j<4;j )
{cout<<"请输入第"<<i<<"行"<<"第"<<j<<"列的值"<<endl;
cin>>temp;
path[1].nodesun[i][j]=temp;
}
cout<<"请输入初始状态的空格的位置(行)"<<endl;
cin>>temp;
path[1].x=temp;
cout<<"请输入初始状态的空格的位置(列)"<<endl;
cin>>temp;
path[1].y=temp;
//目标状态
for(i=1;i<4;i )
for(j=1;j<4;j )
{cout<<"请输入目标状态第"<<i<<"行"<<"第"<<j<<"列的值"<<endl;
cin>>temp;
desti[i][j]=temp;
}
//广度优先搜索
while(front<=rear&&!find)
{ m=path[front].x;
n=path[front].y ;
for(f=0;f<4;f )
{i=m zx[f];
j=n zy[f];
if(i>=1&&i<=3&&j>=1&&j<=3)
{rear ;
path[rear]=path[front];
path[rear].nodesun[m][n]=path[front].nodesun[i][j];
path[rear].nodesun[i][j]=0;
path[rear].pre=front;
path[rear].x=i;
path[rear].y=j;
if(detect(&path[rear]))
{printlj();
find=1;
break;
}
}
}
front ;
}
}

上面是用最简单的深度优先,广度优先直接搜索得来得,毫无AI可言,这并不说明我不能写出其更好得算法,这里最简单得的估价函数f(x)=g(x) h(x)
g(x)用初始化的节点到当前的节点的路径长度来表示h(x)启发函数用当前状态和目标状态的位置不同的个数来表

教程录入:admin    责任编辑:admin 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     
     
     
     

    access基础知识
    免责声明!本站资料大部分来自于互联网,其版权归原作者或其他合法者所有.如内容涉及或侵犯了您的权益,请通知本人,我将尽快处理!.欢迎您的光临。
    辽ICP备07003958号
    无师自通,伴你一生-教程网