您现在的位置:机电论文网>> 机器人>> 正文内容

一个走迷宫算法的源程序

作者: 来源: 发布时间:2011/3/1 9:22:52  点击数:1772
 
#i nclude<iostream>
#i nclude<set>
#i nclude<algorithm>
#i nclude<iterator>
#i nclude<vector>
#i nclude<ctime>
#i nclude<windows.h>
using namespace std;
class node
{
public:
int title;
int h;  //深度
int f;  //代价
node*father;
node(int title=0,int h=0,int f=0,node*father=NULL):
title(title),h(h),f(f),father(father){};
friend bool operator<(const node&s,const node&st)
{
  if(s.f<st.f)return true;
  if(s.f==st.f)return true;
  return false;
}
};
multiset<node>open;
vector<node>close;
int map_w, map_h;
unsigned int*map;
unsigned int*dis_map;
int startx,starty,endx,endy;
inline int title_num(int x,int y){return map_w*x+y;}
inline int title_y(int n){return n%map_w;}
inline int title_x(int n){return n/map_w;}
void maps()
{
int in1;
int in2;
cout<<"请输入随机地图的宽度:";
cin>>in1;
map_w=in1+2;
cout<<"请输入随机地图的高度:";
cin>>in2;
    map_h=in2+2;
cout<<endl;
cout<<"请输入地图的入口坐标x["<<1<<","<<in1<<"]"<<",y["<<1<<","<<in2<<"]nx=";
cin>>endy;
while(endy<1||endy>in1)
{
  cin.clear();
  while(cin.get()!='n')continue;
  cout<<"输入错误,请重新输入nx=";
  cin>>endy;
}
    cout<<"y=";
cin>>endx;
while(endx<1||endx>in2)
{
  cin.clear();
  while(cin.get()!='n')continue;
  cout<<"输入错误,请重新输入ny=";
  cin>>endx;
}
cout<<endl;
cout<<"请输入地图的出口坐标x["<<1<<","<<in1<<"]"<<",y["<<1<<","<<in2<<"]nx=";
cin>>starty;
while(starty<1||starty>in1)
{
  cin.clear();
  while(cin.get()!='n')continue;
  cout<<"输入错误,请重新输入nx=";
  cin>>starty;
}
    cout<<"y=";
cin>>startx;
while(startx<1||startx>in2)
{
  cin.clear();
  while(cin.get()!='n')continue;
  cout<<"输入错误,请重新输入ny=";
  cin>>startx;
}
cout<<endl;
map=new unsigned int[map_w*map_h];
    dis_map=new unsigned int[map_w*map_h*sizeof(dis_map)];
    memset(dis_map,0xff,map_h*map_w*sizeof(*dis_map));
int i=0,j=0,direc=2;
int ran;
for(i=0;i<map_h;i++)
        for(j=0;j<map_w;j++)
            map[map_w*i+j]=1;  
srand(time(0));
    for(i=0;i<map_h;i++)
        for(j=0;j<map_w;j++)
            if(map[map_w*i+j]==1){
                ran=(int)rand()%10;
                if(ran<3)map[map_w*i+j]=0;
   }
   map[title_num(startx,starty)]=1;
   map[title_num(endx,endy)]=1;
   for(i=0;i<map_h;++i)
   {
    for(j=0;j<map_w;++j)
    {
     if(i==0||j==0)map[title_num(i,j)]=0;
     if(i==map_h-1||j==map_w-1)map[title_num(i,j)]=0;
    }
   }
}
int testing(int x,int y)
{
return abs(endx-x)+abs(endy-y);
}
int trytest(int x,int y,node*priot)         
{
int h;
if(0==map[title_num(x,y)])return 1;
h=priot->h+1;
if(h>=dis_map[title_num(x,y)])return 1;
dis_map[title_num(x,y)]=h;
open.insert(node(title_num(x,y),h,h+testing(x,y),priot));
return 0;
}
int*lookup()
{
node*root=new node();
int i;
int*paths;
root->title=title_num(startx,starty);
open.insert(*root);
for(;;)
{
  int x,y,child;
  root=new node();
  if(open.empty())break;
  root->title=open.begin()->title;
     root->h=open.begin()->h;
     root->father=open.begin()->father;
  open.erase(open.begin());
  close.push_back(*root);
  x=title_x(root->title );
  y=title_y(root->title );
  if(x==endx&&y==endy)break;
  child=1;
        child&=trytest(x,y-1,root);                    
  child&=trytest(x,y+1,root);
  child&=trytest(x-1,y,root);
  child&=trytest(x+1,y,root);
        if(child!=0)close.erase(close.end()-1);
}
if(open.empty())return NULL;
paths=new int[root->h+2];
for(i=0;root;++i)
{
  paths=root->title;
  root=root->father;
}
paths=-1;
return paths;
}
void showmap()
{
int i,j;
for(i=0;i<map_h;++i)
{
  for(j=0;j<map_w;++j)
  {
          if(i==0||j==0||i==map_h-1||j==map_w-1)
    {
     cout<<"■";
     continue;
    }
    if(i==startx&&j==starty)
    {
     cout<<"E";
     continue;
    }
    if(i==endx&&j==endy)
    {
     cout<<"S";
     continue;
    }
    if(map[map_w*i+j]=='*')
    {
     cout<<"*";
     continue;
    }
    if(map[map_w*i+j])cout<<"□";
    else cout<<"■";

  }
  cout<<endl;
}
Sleep(800);
}
void showpath(int *paths)
{
for(int i=0; paths>0; ++i)
{
  map[paths]='*';
  system("cls");
  showmap();
}
}
int main()
{
while(1)
{
         char anyt;
      int*paths;
      maps();
   system("CLS");
      showmap();
   system("cls");
      showmap();
      system("PAUSE");
      paths=lookup();
      if(paths==NULL)cout<<"没路"<<endl;
   if(paths)showpath(paths);
   if(paths)delete []  paths;
   if(dis_map)delete [] dis_map;
         if(map)delete []  map;
   cout<<"再来一次(Y/N)?";
      cin>>anyt;
      if(anyt=='n'||anyt=='N')break;
}
system("PAUSE");
return 0;
}
 

更多
字体:【】-【】-【】【关闭此页

上一篇:小型双足步行机器人的结构及其控制电路'   下一篇:TD计费模式向多元化发展'


特别声明:机电之家(http://www.jdzj.com )所共享的机电类资料,机电论文、机电类文章、机电企业类管理制度、机电类软件都来自网上收集,其版权归作者本人所有,如果有任何侵犯您权益的地方,请联系我们,我们将马上进行处理。购买的论文都出自原创,保证作者的原创的版权的转让,任何纠纷由法律解决。