迷宫。。。
来源:互联网 发布:常见网络拓扑结构图 编辑:程序博客网 时间:2024/04/28 04:51
//============================================================================
// Name : 迷宫.cpp
// Author : mtt
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define M 8
#define N 11
typedef struct
{
int x;
int y;
int d;
}Datatype;
struct seqstack
{
int Maxsize;
int top;//指示栈定位置
Datatype *s;
};
typedef struct seqstack *pseqstack;
void createEmptystack(seqstack &s ,int m,int n)
{
s.Maxsize=m*n;
s.top=-1;
s.s=new Datatype[m*n];
}
void push_seq(pseqstack pastack, Datatype x)
{
if(pastack->top==pastack->Maxsize-1)
{
cout<<"stack is full";
exit(1);
}
pastack->top++;
pastack->s[pastack->top]=x;
}
Datatype pop(pseqstack pastack)
{
if(pastack->top==-1)
{cout<<"栈为空"<<endl;
exit (1);}
else
return pastack->s[pastack->top];
}
int is_Empty(pseqstack pastack)
{
if (pastack->top==-1)
return 1;
else return 0;
}
void mazepath(pseqstack pastack,int m,int n,int x1,int y1,int x2,int y2,int direct[][2],int maze[][11])
{
createEmptystack(*pastack,m,n);
int g,h;
int i,j,k;
maze[x1][y1]=2;//标记已经走过
Datatype element ;
element.x=x1;
element.y=y1;
element.d=-1;
push_seq(pastack,element);
while(!is_Empty(pastack))
{
element=pop(pastack);
i=element.x;
j=element.y;
k=element.d;
while (k<=3)
{
g=maze[i][k]+direct[k][0];
h=maze[j][k]+direct[k][1];
if(g==x2&&h==y2&&maze[g][h]==0)
{
cout<<"the reverse path is"<<endl;
cout<<"the node is"<<g<<","<<h<<" ";
cout<<"the node is"<<i<<","<<j<<" ";
while (!is_Empty(pastack))
{
element=pop(pastack);
cout<<""<<element.x<<","<<element.y<<" ";
}
free(pastack->s);
free(pastack);
return ;
}
if (maze[g][h]==0)
{
maze[g][h]=2;
element.x=i;
element.y=j;
element.d=k;
push_seq(pastack,element);
i=g;
j=h;
k=-1;
}
k++;
}
}
cout<<"no path find"<<endl;
free(pastack->s);
free(pastack);
}
int main() {
int direct[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
seqstack s;
pseqstack p=&s;
int maze [M][N]={{1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,0,1,1,1,0,0,1},
{1,0,0,0,0,0,1,0,0,1,1},
{1,0,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,1,1,0,1,1},
{1,1,0,0,1,0,1,1,0,0,1},
{1,1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1}};
mazepath(p,8,11,1,1,6,9,direct,maze);
return 0;
}
// Name : 迷宫.cpp
// Author : mtt
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define M 8
#define N 11
typedef struct
{
int x;
int y;
int d;
}Datatype;
struct seqstack
{
int Maxsize;
int top;//指示栈定位置
Datatype *s;
};
typedef struct seqstack *pseqstack;
void createEmptystack(seqstack &s ,int m,int n)
{
s.Maxsize=m*n;
s.top=-1;
s.s=new Datatype[m*n];
}
void push_seq(pseqstack pastack, Datatype x)
{
if(pastack->top==pastack->Maxsize-1)
{
cout<<"stack is full";
exit(1);
}
pastack->top++;
pastack->s[pastack->top]=x;
}
Datatype pop(pseqstack pastack)
{
if(pastack->top==-1)
{cout<<"栈为空"<<endl;
exit (1);}
else
return pastack->s[pastack->top];
}
int is_Empty(pseqstack pastack)
{
if (pastack->top==-1)
return 1;
else return 0;
}
void mazepath(pseqstack pastack,int m,int n,int x1,int y1,int x2,int y2,int direct[][2],int maze[][11])
{
createEmptystack(*pastack,m,n);
int g,h;
int i,j,k;
maze[x1][y1]=2;//标记已经走过
Datatype element ;
element.x=x1;
element.y=y1;
element.d=-1;
push_seq(pastack,element);
while(!is_Empty(pastack))
{
element=pop(pastack);
i=element.x;
j=element.y;
k=element.d;
while (k<=3)
{
g=maze[i][k]+direct[k][0];
h=maze[j][k]+direct[k][1];
if(g==x2&&h==y2&&maze[g][h]==0)
{
cout<<"the reverse path is"<<endl;
cout<<"the node is"<<g<<","<<h<<" ";
cout<<"the node is"<<i<<","<<j<<" ";
while (!is_Empty(pastack))
{
element=pop(pastack);
cout<<""<<element.x<<","<<element.y<<" ";
}
free(pastack->s);
free(pastack);
return ;
}
if (maze[g][h]==0)
{
maze[g][h]=2;
element.x=i;
element.y=j;
element.d=k;
push_seq(pastack,element);
i=g;
j=h;
k=-1;
}
k++;
}
}
cout<<"no path find"<<endl;
free(pastack->s);
free(pastack);
}
int main() {
int direct[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
seqstack s;
pseqstack p=&s;
int maze [M][N]={{1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,0,1,1,1,0,0,1},
{1,0,0,0,0,0,1,0,0,1,1},
{1,0,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,1,1,0,1,1},
{1,1,0,0,1,0,1,1,0,0,1},
{1,1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1}};
mazepath(p,8,11,1,1,6,9,direct,maze);
return 0;
}
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫?
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫。。。
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- A*智能寻路算法(php版本)
- 黑马程序员之jquery学习笔记:jquery特效之金额文本框代码
- php 循环table,让内容一行两列显示
- ipad 中获取相册中图片名称
- hibernate二级缓存配置文件ehcache.xml属性意义
- 迷宫。。。
- windows 下vim taglist插件的安装,真实可用
- 库
- try to use recycled bitmap
- 关于Excel操作编写的一个软件设计构思案例[连载] --保存条件设定的另一种方式:更新与插入方法
- JNI 的 local reference table overflowed 问题
- Nginx的epoll模块
- android 滚动条 相关属性
- Rcp应用在启动时出现找不到org.eclipse.equinox.launcher.Main类错误