JAVA迷宫代码堆栈算法 java迷宫求解

我的迷宫代码帮忙改一下,只修改就行了,写完整啊,很急啊JAVA迷宫代码堆栈算法你可以试试这个
#includestdio.h
#includestdlib.h
#define M 10 //自己规定为10*10JAVA迷宫代码堆栈算法的迷宫
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
int findway(int);
int NextStep(int *, int *, int );
typedef struct
{
int x, y;//坐标
int dir;//方向
}ElemType;
typedef struct StackNode//构造栈
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)//初始化栈
{
S-base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S-base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
S-top=S-base;
S-stacksize=STACK_INIT_SIZE;
return OK;
}
int Push(SqStack *S,ElemType e)//进栈操作
{
if(S-top-S-base=S-stacksize)
{
S-base = (ElemType *)realloc(S-base,(S-stacksize STACKINCREMENT)*sizeof(ElemType));
if (!S-base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
S-top = S-base S-stacksize;
S-stacksize= STACKINCREMENT;
}
*S-top=e;
return OK;
}
int Pop(SqStack *S,ElemType *e)//出栈操作
{
if(S-top==S-base)
{
return ERROR;
}
*e=*--S-top;
//printf("%d\n",e);
return OK;
}
int StackEmpty(SqStack *S)//判断栈是否为空
{
if(S-top==S-base)
return OK;
else
return ERROR;
}
void Input(char b[M][M])//输入时候请注意把一圈都输入为墙即'#'
{
int i, j;
printf("请输入迷宫形状:\n");
for (i = 0; iM; i)
{
for (j = 0; jM; j)
{
scanf("%c",b[i][j]);
}
getchar();//吃掉内存中的残留换行符号
}
}
void Ouput(const char b[M][M])
{
int i, j;
printf("迷宫的形状为:\n");
for (i = 0; iM; i)
{
for (j = 0; jM; j)
{
printf("%c",b[i][j]);
}
printf("\n");
}
}
int FindWay(char maze[M][M])
{
ElemTypee;
int constep = 1;
int x = 1, y = 1;
SqStack S;
InitStack(S);
do
{
if (maze[x][y] == ' ')//当第3次时 maze[x][y]!=' ' 照样通不过
{
maze[x][y] = '1';
e.x = x;
e.y = y;
e.dir = 1;
Push(S,e);
if (x == M-2y == M-2)
{
printf("存在出口\n");
return 1;
}
NextStep(x,y,1);
constep;
}
else
{
Pop(S,e);
while (e.dir == 4!StackEmpty(S))
{
maze[e.x][e.y] = '0';
Pop(S,e);
}
{
if (e.dir4)
{
e.dir;
Push(S,e);
x = e.x;
y = e.y;
NextStep(x, y, e.dir);
}
else
{
printf("没有出口\n");
return 0;
}
}
}
}while(S.top!=S.base);
return 0;
}
int NextStep(int *x, int *y, int dir)
{
switch(dir)
{
case 1:
(*y);
break;
case 2:
(*x);
break;
case 3:
(*y)--;
break;
case 4:
(*x)--;
break;
default:
break;
}
return 0;
}
int main(void)
{
char a[M][M];
Input(a);
Ouput(a);
FindWay(a);
Ouput(a);
system("pause");
return 0;
}
【迷宫问题】用堆栈解迷宫#include stdio.h
#include time.h
#include stdlib.h
#include string.h
#define MAXM 40
#define MAXN 60
int maze[MAXM][MAXN]; // 0 1-障碍物
int m = 40, n = 60; // 40行60列
int used[MAXM][MAXN]; // 记录是否到过
int line[MAXM*MAXN]; // 记录迷宫路线
int line_len;
int line_r[MAXM*MAXN];
int d[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1} }; // 4个方向
void dfs(int u, int v, int step)
{
int x, y, i;
if (line_len != 0)
return;
if (u == m-1v == n-1)
{
line_len = step;
memcpy(line_r, line, line_len*sizeof(int));
return;
}
for (i = 0; i4; i)
{
x = ud[i][0];
y = vd[i][1];
if (x = 0xmy = 0yn!used[x][y]maze[x][y]==0)
{
used[x][y] = 1;
line[step] = x16 | y;
dfs(x, y, step 1);
// used[x][y] = 0; // 不用退回来了,因为只需要一条路径
}
}
}
int main()
{
int i, j;
// 随机生成迷宫
srand(time(NULL));
for (i = 0; im; i)
for (j = 0; jn; j)
maze[i][j] = rand()%8 == 0 ? 1 : 0; // 是1的概率约1/8,通路的概率大些
maze[0][0] = maze[m-1][n-1] = 0; // 起始点和终端必须为0
line_len = 0;
line[0] = 016 | 0;
memset(used, 0, sizeof(used));
dfs(0, 0, 0); // (0,0) - (m-1, n-1)
if (line_len == 0)
printf("没有通路\n");
else
{
for (i = 0; iline_len; i)
printf("( %d, %d )\n", (line_r[i]16) 1, (line_r[i]0xffff) 1);
}
return 0;
}
你给的那库实在是没什么用的欲望,要最短路径一般用广度搜索,上面的代码应该属于深度搜索
Java如何实现堆栈//这是JDK提供的栈
import java.util.Stack;
public class UsingStack {
public static void main(String[] args) {
//构造栈对象,使用类型限制,只能存储Integer数据
StackInteger s = new StackInteger();
//1、2、3依次入栈
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出栈
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
//这是我写的顺序结构的栈
import java.util.EmptyStackException;
import java.util.Vector;
public class UsingStack{
public static void main(String[] args){
//构造栈对象 , 使用类型限制,只能存储Integer数据
MyStackInteger s = new MyStackInteger();
//1、2、3依次入栈
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出栈
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
/**
* 栈类
* @author developer_05
* @param T
*/
class MyStackT extends VectorT{
/**
* 构造方法
*/
public MyStack(){
}
/**
* 入栈方法
* @param item 待入栈的元素
* @return 返回入栈的元素
*/
public T push(T item) {
addElement(item);
return item;
}
/**
* 出栈方法(同步处理)
* @return 返回出栈元素
*/
public synchronized T pop() {
T obj;
int len = size();
if (len == 0)
throw new EmptyStackException();
obj = elementAt(len - 1);
removeElementAt(len - 1);
return obj;
}
/**
* 判断栈是否为空的方法
* @return 返回true(栈空)或false(栈非空)
*/
public boolean empty() {
return size() == 0;
}
private static final long serialVersionUID = 1L;
}
求助 java一个二维数组代表迷宫 。0代表道路 2表示墙壁 。假设老鼠会从数组[1][0]开始这个可以用 堆栈 来完成 。
用堆栈的基本思路就是 。
设置一个起点A 。将 A 入栈。
从A开始找到第一个可以达到的点B 。将 B 入栈。
如果B无路可走 。则在A点处重新换一个可达到的点 。否则继续 2-3。直到达到终点 。或者五路可走 。
详细的解释,这儿有一篇博文:
【JAVA迷宫代码堆栈算法 java迷宫求解】关于JAVA迷宫代码堆栈算法和java迷宫求解的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读