subject_56655.htm
来自「vc」· HTM 代码 · 共 24 行
HTM
24 行
<p>
序号:56655 发表者:linyb 发表日期:2003-10-19 21:14:50
<br>主题:谁能告诉我迷宫问题的源代码(用C语言写).
<br>内容:请教个位专家:谁能告诉我迷宫问题的源代码(用C语言写).<BR>谢谢!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:jfkuo 回复日期:2003-10-21 13:44:39
<br>内容://有路径tc3.0通过<BR>#define null (void*)(0)<BR>#define m 6<BR>#define n 8<BR><BR>int maze[m+2][n+2]=<BR>{<BR>1,1,1,1,1,1,1,1,1,1,<BR>1,0,1,1,1,0,1,1,1,1,<BR>1,1,0,1,0,1,1,1,1,1,<BR>1,0,1,0,0,0,0,0,1,1,<BR>1,0,1,1,1,0,1,1,1,1,<BR>1,1,0,0,1,1,0,0,0,1,<BR>1,0,1,1,0,0,1,1,0,1,<BR>1,1,1,1,1,1,1,1,1,1<BR>};//maze,0:pass;1:no pass;<BR><BR>typedef struct<BR>{<BR> int x,y;<BR>}item;<BR><BR>item move[8]=<BR>{<BR> {0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}<BR>};<BR><BR>typedef struct<BR>{<BR> int x,y,d;<BR>}datatype;<BR><BR>typedef struct node<BR>{<BR> datatype data;<BR> struct node *next;<BR>}StackNode,*LinkStack;<BR><BR>LinkStack Init_LinkStack()<BR>{<BR> return null;<BR>}<BR>int Length_LinkStack(LinkStack s)<BR>{<BR> StackNode *p=s;<BR> int len=0;<BR> while(p!=null){len++;p=p->next;}<BR> return len;<BR>}<BR>int Empty_LinkStack(LinkStack s)<BR>{<BR> if(s==null) return 1;<BR> else return 0;<BR>}<BR>LinkStack Push_LinkStack(LinkStack s,datatype inData)<BR>{<BR> StackNode *tmp;<BR> tmp=(StackNode *)(malloc(sizeof(StackNode)));//no consideration for the failure of malloc().<BR> tmp->data=inData;<BR> tmp->next=s;<BR> s=tmp;<BR> return s;<BR>}<BR>LinkStack Pop_LinkStack(LinkStack s,datatype *outData)<BR>{<BR> StackNode *tmp;<BR> if(s==null) return null;<BR> else<BR> {<BR> *outData=s->data;<BR> tmp=s;<BR> s=s->next;<BR> free(tmp);<BR> return s;<BR> }<BR>}<BR><BR>int path(int maze[m+2][n+2],item move[8])<BR>{<BR> int i,j,x,y,d;<BR> StackNode *p;<BR> LinkStack s;<BR> datatype temp;<BR> s=Init_LinkStack();<BR> temp.x=1;temp.y=1;temp.d=-1;<BR> s=Push_LinkStack(s,temp);<BR> while(!Empty_LinkStack(s))<BR> {<BR> s=Pop_LinkStack(s,&temp);<BR> x=temp.x;y=temp.y;d=temp.d+1;<BR> while(d<8)<BR> {<BR> i=x+move[d].x;j=y+move[d].y;<BR> if(maze[i][j]==0)<BR> {<BR> temp.x=x;temp.y=y;temp.d=d;<BR> s=Push_LinkStack(s,temp);<BR> x=i;y=j;maze[x][y]=-1;<BR> if(x==m&&y==n)<BR> {<BR> temp.x=m;temp.y=n;temp.d=-1;<BR> s=Push_LinkStack(s,temp);//add the exit to the stack.<BR> while(s!=null)<BR> {<BR> p=s;<BR> printf("%d,%d\n",s->data.x,s->data.y);<BR> s=s->next;<BR> free(p);<BR> }<BR> return 1;<BR> }<BR> else d=0;<BR> }<BR> else d++;<BR> }//while(d<8)<BR> }//while<BR> return 0;<BR>}<BR><BR>void main()<BR>{ <BR> path(maze,move);<BR> getch();<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:jfkuo 回复日期:2003-10-21 13:45:48
<br>内容://最短路径tc3.0通过<BR>#define m 6<BR>#define n 8<BR>#define num 48<BR><BR>int maze[m+2][n+2]=<BR>{<BR>1,1,1,1,1,1,1,1,1,1,<BR>1,0,0,0,1,0,1,1,1,1,<BR>1,0,1,1,0,1,1,1,1,1,<BR>1,0,1,0,1,0,0,0,1,1,<BR>1,0,1,1,1,0,0,0,1,1,<BR>1,1,0,0,1,1,0,0,0,1,<BR>1,0,1,1,0,0,0,0,0,1,<BR>1,1,1,1,1,1,1,1,1,1<BR>};//maze,0:pass;1:no pass;<BR><BR>typedef struct<BR>{<BR> int x,y;<BR>}item;<BR>item move[8]=<BR>{<BR> {0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}<BR>};<BR><BR>typedef struct<BR>{<BR> int x,y;<BR> int pre;<BR>}sqtype;<BR><BR>void printpath(sqtype sq[num],int rear)/*print the path from the queue*/<BR>{<BR> int i;<BR> i=rear;<BR> do<BR> {<BR> printf("(%d,%d)<-",sq[i].x,sq[i].y);<BR> i=sq[i].pre;/*trace back*/<BR> }while(i!=-1);<BR>} /*printpath*/<BR><BR>int path(int maze[m+2][n+2],item move[8])<BR>{<BR> int x,y,i,j,v;<BR> sqtype sq[num];<BR> int front,rear;<BR> front=rear=0;<BR> sq[0].x=1;sq[0].y=1;sq[0].pre=-1;/*the entrance node of the queue*/<BR> maze[1][1]=-1;<BR> while(front<=rear)/*queue is not null*/<BR> {<BR> x=sq[front].x;y=sq[front].y;<BR> for(v=0;v<8;v++)<BR> {<BR> i=x+move[v].x;j=y+move[v].y;<BR> if(maze[i][j]==0)<BR> {<BR> rear++;<BR> sq[rear].x=i;sq[rear].y=j;sq[rear].pre=front;<BR> maze[i][j]=-1;<BR> }<BR> if (i==m&&j==n)<BR> {<BR> printpath(sq,rear);/*print the path*/<BR> //restore(maze);/*restore the maze:0 or 1*/<BR> return 1;<BR> }<BR> }/*for v*/<BR> front++;/*search the next node*/<BR> }/*while*/<BR> return 0;<BR>}/*path*/<BR><BR>void main()<BR>{ printf("\n");<BR> path(maze,move);<BR> getch();<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:wangyy 回复日期:2003-10-22 21:24:51
<br>内容:能否加一些注释,让我们也了解了解。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?