是高手就进来啊!

h202hDisney 2008-04-12 01:13:13
可以输入一个任意大小的迷宫数据,用非递归的方法求出一条走出迷宫的路径,并将路径输出;

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

void main()
{

int n;
int i;
int j;
int **p;
printf("input the size of the map\n");
scanf("%d",&n);
srand((unsigned)time(NULL));
p=(int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
{
p[i]=(int*)malloc(sizeof(int)*n);
}


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
p[i][j]=rand()%2;
printf("%d ",p[i][j]);
}
puts("");
}

}
这只是开始,随机生成了二维数组.迷宫希望是随机生成的.用户输入的数据只是迷宫大小.
迷宫想用1来表示墙.0表示通路.
用非递归的方法求出一条走出迷宫的路径,并将路径输出~~~
...全文
110 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
vagrantfish 2008-04-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 baihacker 的回复:]
是高手就进来啊!
高手一定会做的!!!!

顺便指出,下次不要用这种标题了,这样的标题一般没有人进来看的,即使看了也不愿意帮你的.
拿着上面的代码交作业吧,顺便给你的同学几份
还不如直接说:帮做作业,在线等 之类的耿直

你也不用给分,随便给个无满意答案结贴就是了.
如果有所愧意就看看这个贴子:
http://topic.csdn.net/u/20080412/00/8d4590bd-34d0-41b3-9566-9306f6f73154.html?878166406
[/Quote]
  • 打赏
  • 举报
回复
不是高手,路过,呵呵!新人.
  • 打赏
  • 举报
回复
你给300分,人气就很高,就算标题写空格都没问题
就呆在云上 2008-04-12
  • 打赏
  • 举报
回复
我进来了
用户 昵称 2008-04-12
  • 打赏
  • 举报
回复
在这里,分多就是吸引人的标题。
h202hDisney 2008-04-12
  • 打赏
  • 举报
回复
SORRY.第一次上这个论坛.也不知道要怎么拟标题才能吸引别人的注意.
现在回头想,这两个标题拟得有点嚣张.
其实只是想能在最短时间吸引到别人注意罢了.
考虑不周全.得罪了...

代码我会认真看.谢谢.
baihacker 2008-04-12
  • 打赏
  • 举报
回复
是高手就进来啊!
高手一定会做的!!!!

顺便指出,下次不要用这种标题了,这样的标题一般没有人进来看的,即使看了也不愿意帮你的.
拿着上面的代码交作业吧,顺便给你的同学几份
还不如直接说:帮做作业,在线等 之类的耿直

你也不用给分,随便给个无满意答案结贴就是了.
如果有所愧意就看看这个贴子:
http://topic.csdn.net/u/20080412/00/8d4590bd-34d0-41b3-9566-9306f6f73154.html?878166406
baihacker 2008-04-12
  • 打赏
  • 举报
回复


#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#define OVERFLOW -1
#define MAX 100
#define N 100

typedef struct
{
int x;
int y;
int d;
}Data;

typedef struct
{
int pos;
Data data[MAX];
}SNode,*Stack;

Stack InitStack()
{
Stack pStack;
pStack=(Stack)malloc(sizeof(SNode));
if(!pStack)
exit(OVERFLOW);
pStack->pos=-1;
return pStack;
}

int IsEmpty(Stack stack)
{
return stack->pos==-1;
}

void Push(Stack pStack,Data x)
{
if(pStack->pos>=MAX-1)
exit(OVERFLOW);
else
{
pStack->pos++;
pStack->data[pStack->pos]=x;
}
}

void Pop(Stack pStack)
{
if(pStack->pos==-1)
exit(OVERFLOW);
else
pStack->pos--;
}

Data GetTop(Stack pStack)
{
return pStack->data[pStack->pos];
}

Data SetStackElem(int x,int y,int d)
{
Data element;
element.x=x;
element.y=y;
element.d=d;
return element;
}

void DisplayPath(Stack pStack)
{
Data element;
printf("The path is:\n");
while(!IsEmpty(pStack)) //多了个;
{
element=GetTop(pStack);
Pop(pStack);
printf("The node is: %d %d\n",element.x,element.y);
}
}

void MazePath(int maze[][N],int direction[][2],int x1,int y1,int x2,int y2)
{
int i,j,k,g,h;
Stack pStack;
Data element;
pStack=InitStack();
maze[x1][y1]=2;
Push(pStack,SetStackElem(x1,y1,0));

while(!IsEmpty(pStack))//以前多了一个;
{
element=GetTop(pStack);
Pop(pStack);
i=element.x;
j=element.y;
k = element.d;

while (k<=3)//用while
{
g=i+direction[k][0];
h=j+direction[k][1];
if(g==x2&&h==y2&&maze[g][h]==0)
{
Push(pStack,SetStackElem(i,j,k));//把路径上最后一个点放入
DisplayPath(pStack);
return;//怎么会有返回值?
}

if(maze[g][h]==0)
{
maze[g][h]=2;
Push(pStack,SetStackElem(i,j,k+1));
i=g;
j=h;
k=0;
}
else
k++;
}
}
printf("The path has not been found\n");
}


void main()
{

int n;
int i;
int j;
int m[N][N];
int direction[][2]={0,1,1,0,0,-1,-1,0};
do{
printf("input the size of the map\n");
scanf("%d",&n);
} while(n+2>N||n<1);

srand(time(NULL));
for(i=0;i<=n+1;i++)
{
for(j=0;j<=n+1;j++)
{
if (i==0||i==n+1||j==0||j==n+1) m[i][j] = 1;
else m[i][j] = rand()%2;

}
}
m[1][1]=m[n][n]=0;
for (i = 0; i <= n+1;puts(""), ++i)
for (j = 0; j <= n+1;printf("%d\t", m[i][j]), ++j)
;
MazePath(m,direction,1,1,n,n);
}

input the size of the map
3
1 1 1 1 1
1 0 1 0 1
1 0 0 0 1
1 0 1 0 1
1 1 1 1 1
The path is:
The node is: 2 3
The node is: 2 2
The node is: 2 1
The node is: 1 1
Press any key to continue
(路径倒着看)

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧